MySQL语法错误; "查看与您的MySQL服务器版本对应的手册.."

Law*_*nce 1 php mysql sql mysql-error-1064

我一直在互联网上寻找以下错误的解决方案;

"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"
Run Code Online (Sandbox Code Playgroud)

我不知道发生了什么......我知道你会问我的代码是什么:

$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);
Run Code Online (Sandbox Code Playgroud)

我一直试图弄清楚大约4/5小时,但没有成功.
谢谢,
劳伦斯

Pas*_*TIN 7

primary在SQL中是一个保留关键字,这意味着您应该:

  • 重命名该列 - 这是一个好主意,以避免这种情况
  • 或使用该名称的反引号

这是第二种情况下查询的样子:

INSERT INTO details (`primary`, `username`, `password`, `password2`)
VALUES (null, 'hello', 'hello', 'hello')
Run Code Online (Sandbox Code Playgroud)


注意:您应该使用mysql_real_escape_string避免SQL注入来逃避您的值!