Pranav Kumar Pranav Kumar - 1 year ago 49
SQL Question

MySQL Insert via php is showing an error

I tried to INSERT values into an sql database via php. There is no need to check "database.php" as it works. It shows

Error: unexpected '$myquery' (T_STRING).

I'm using xampp v3.2.1 as a localhost. NEED HELP FAST.THANKS!!!


require 'database.php';

$for= $_GET ["for"];
$approved= $_GET ["approved"];
$before= $_GET ["before"]

$myquery="INSERT INTO request('request date', 'request for', 'approved by') VALUES (NOW(),'$for','$approved')";
$query = mysql_query($myquery);

Answer Source

Just like Daan said, you forgot a semicolon. You'll need to make this edit:

$before = $_GET["before"];
// note the semicolon at the end of the line

Also, you should use back ticks to quote your field names like so:

$myquery = "INSERT INTO request(`request date`, `request for`, `approved by`) VALUES (NOW(),'$for','$approved')";

One final suggestion, you should move away from using the mysql_* functions to using either their mysqli_* functions or PDO.

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download