Search code examples
phpmysqlinsert-query

PHP/MySQL Insert Query


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.


Solution

  • 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 ``