PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a form that submits itself to the php script on the same page. Inside the php script, there is a class called mysql, and I am wondering why this is not working.
Note: In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).
class MyClass
{ // won't work
var $database = $_POSTї'database'];
var $table = $_POSTї'table'];
var $width = $_POSTї'width'];
...
}
class MyClass
{ // this should
var $database;
var $table;
var $width;
function MyClass()
{
$this->database = $_POST['database'];
$this->table = $_POST['table'];
$this->width = $_POST['width'];
}
...
}
It could be that it makes life simpler in your script if you declare class variables for each of the POST vars, but you could just access them directly with $_POST['varname'] wherever they're needed - saves the overhead of creating extra vars.
You maybe didn't post all your code: would you need to do some checks in the constructor to prevent hackers accessing any old table or database?