4

For the life of me I can't get this insert query to work.

mysql_connect("**host**", "**username**", "**password**") or error("Could not connect: ".mysql_error());
mysql_select_db("**db_name**");
$db = mysql_query("INSERT INTO `pass_reset` (id,status,key,email) VALUES ('','0','$key','$email')");

It returns this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key,email) VALUES ('','0','','')' at line 1

Could someone help me with this? I'm literally pulling my hair out over this simple query.

Spencer
  • 597
  • 2
  • 11
  • 19

4 Answers4

10

Try the following:

$db = mysql_query("INSERT INTO `pass_reset` (id,status,`key`,email) VALUES ('','0','$key','$email')");

Because key is a reserved word by MySQL, you must escape it with the backticks ``

Shef
  • 44,808
  • 15
  • 79
  • 90
3

KEY is a reserved word in MySQL, so you'd have to escape it with back ticks.

Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235
0

Maybe try enclosing the column names with the grave accent?

(`id`,`status`,`key`,`email`)
Will
  • 1,621
  • 15
  • 20
-1

dont put php variable in '', it will surely work man

$db = mysql_query("INSERT INTO `pass_reset` (id,status,key,email) VALUES ('','0',$key,$email)");

Or

 $db = mysql_query("INSERT INTO `pass_reset` (id,status,key,email) VALUES ('0',$key,$email)");
theGame
  • 383
  • 4
  • 6
  • 18