简体   繁体   English

执行mysqli_query时出现SQL语法错误

[英]SQL Syntax Error when executing mysqli_query

I have an PHP registration script with MySQLi and OOP. 我有一个MySQLi和OOP的PHP注册脚本。 But i get an mysql syntax error when executing an query. 但是执行查询时出现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 Code: 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)";

Wheres the problem? 问题出在哪里?

...for the right syntax to use near '-mail  
SQL's telling you where error starts ^ the offending character 

You need to wrap/encapsulate the e-mail column in backticks since it contains a hyphen. 您需要将e-mail列包装/封装在反引号中,因为它包含连字符。

SQL figures you want to do math which translates to: e minus mail 您要进行数学运算的SQL数字,它转换为: email

Plus, missing quotes in your values 另外,您的值中缺少引号

$register_sql = "INSERT INTO users (id, username, password, pin, `e-mail`, ip_register, ip_lastlogin, lastlogin) VALUES ('', '$username', '$password', '$pin', '$email', '$ip', '$ip', '$lastlogin')";

Those are strings and must be inside quotes. 这些是字符串,必须在引号内。

  • Another option would be to rename your column to e_mail using an underscore as you did for some of the other columns. 另一个选择是像在其他一些列中一样,使用下划线将您的列重命名为e_mail That way, you would not need to use backticks. 这样,您将不需要使用反引号。

Look into using one of the following also: 还可以考虑使用以下之一:


Having used or die(mysqli_error($con)) to mysqli_query() would have signaled the error(s). mysqli_query()使用过or die(mysqli_error($con)) mysqli_query()会发出错误信号。

$con being your DB connection, this could/stand to be different than yours. $con是您的数据库连接,这可能/立场与您的不同。

  • Adjust accordingly. 相应地调整。

Identifiers (table/columns) 标识符 (表/列)


Tip: 小费:

Try and avoid using hyphens, or spaces or any other character that SQL may complain about, this includes using a space in between words. 尝试避免使用连字符,空格或SQL可能抱怨的任何其他字符,这包括在单词之间使用空格。

Ie: 即:

INSERT INTO your_table (column 1, column-2) <= will cause/throw an error

you would need to use backticks: 您将需要使用反引号:

INSERT INTO your_table (`column 1`, `column-2`) <= correct / valid

Although spaces are allowed ( yet discouraged ), they too need to be encapsulated in backticks. 尽管允许( 但不鼓励 )空格,但也需要将其封装在反引号中。

If you're going to have a dash in a column identifier (which is a bad idea) you must wrap it in ticks. 如果您要在列标识符中使用破折号(这是个坏主意),则必须将其包装在刻度中。 Otherwise you are subtracting the value of the mail column from the e column which not not valid in an INSERT statement. 否则,您将从e列中减去在INSERT语句中无效的mail列的值。

You're also missing quotes around your string values. 您还缺少字符串值周围的引号。

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

I suppose your id is set to Auto Increment . 我想您的ID设置为自动增量 If it is just remove the first column from the insert statement and it should work fine. 如果只是从插入语句中删除第一列,它应该可以正常工作。

$register_sql = "INSERT INTO users (username, password, pin, e-mail, ip_register, ip_lastlogin, lastlogin) VALUES ($username, $password, $pin, $email, $ip, $ip, $lastlogin)";

And yes, change the e-mail field to `e-mail`. 是的,将“电子邮件”字段更改为“电子邮件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM