Search code examples
phpsqlmysqlmysql-error-1064

MySQL Syntax Error; "check the manual that corresponds to your MySQL server version.."


I've been looking all over the internet for a solution to the following 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 'primary, username, password, password2) VALUES (null, 'hello', 'hello', 'hello')' at line 1"

I have no idea what is going on.. I know you will ask what my code is so here:

$con = mysql_connect("localhost","root","*****");
    if (!$con)
      {
      die('Server overload, please try again' . mysql_error());
      }

    mysql_select_db("users", $con);

    $sql = "INSERT INTO details (primary, username, password, password2) VALUES (null, '$_POST[username]', '$_POST[password]', '$_POST[password2]')";

    if (!mysql_query($sql,$con))
      {
      die('Error: Server overload, try again' . mysql_error());
      }
    echo "You have signed up successfully!";

    mysql_close($con);

I've been trying to figure it out for around 4/5 hours now and have had no success.
Thanks,
Lawrence


Solution

  • primary is a reserved keyword, in SQL, which means that you should either :

    • rename that column -- would be a good idea, to avoid that kind od situation
    • or use backticks arround that name

    Here what the query would look like in the second case :

    INSERT INTO details (`primary`, `username`, `password`, `password2`)
    VALUES (null, 'hello', 'hello', 'hello')
    


    Note : and you should escape your values, using mysql_real_escape_string, to avoid SQL Injections !