我有一个MySQLi和OOP的PHP注册脚本。但是执行查询时出现mysql语法错误。
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 '-mail, ip_register, ip_lastlogin, lastlogin) VALUES ('', Aarivex, ******, ****' at line 1
PHP代码:
$register_sql = "INSERT INTO users (id, username, password, pin, e-mail, ip_register, ip_lastlogin, lastlogin) VALUES ('', $username, $password, $pin, $email, $ip, $ip, $lastlogin)";
问题出在哪里?
...for the right syntax to use near '-mail
SQL's telling you where error starts ^ the offending character
由于e-mail
列中包含连字符,因此您需要将其包装/封装在反引号中。
您要进行数学运算的SQL数字,它转换为:e
减号mail
另外,您的值中缺少引号
$register_sql = "INSERT INTO users (id, username, password, pin, `e-mail`, ip_register, ip_lastlogin, lastlogin) VALUES ('', '$username', '$password', '$pin', '$email', '$ip', '$ip', '$lastlogin')";
这些是字符串,必须在引号内。
e_mail
使用下划线,就像处理其他一些列一样。这样,您将不需要使用反引号。还可以考虑使用以下之一:
已经使用or die(mysqli_error($con))
到mysqli_query()
会发出信号错误(或多个)。
$con
作为您的数据库连接,这可能/与您的不同。
标识符(表/列)
小费:
尝试避免使用连字符,空格或SQL可能抱怨的任何其他字符,这包括在单词之间使用空格。
IE:
INSERT INTO your_table (column 1, column-2) <= will cause/throw an error
您将需要使用反引号:
INSERT INTO your_table (`column 1`, `column-2`) <= correct / valid
尽管允许(但不鼓励)空格,但也需要将其封装在反引号中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句