PHP :您的 SQL 语法有误,请查看手册
PHP : You have an error in your SQL syntax check the manual
我正在尝试通过提交表单来插入数据,这是我收到的错误
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 'desc,refno,quantity,imgname, image)
VALUES('name','Some desc ', '123','123' at line 1
以下是我的 sql 查询
$query= mysqli_query($connect,
"INSERT INTO products(name,desc,refno,quantity)
VALUES('$names','$productdesc', '$refno','$quanitity')");
以下是数据库的附加屏幕截图
如果我调用 print_r
这就是我得到的结果
Array
(
[name] => name
[refno] => 123
[quantity] => 123456
[description] => Some desc
[submit] => Add Product
)
因为 desc
是一个保留字,你需要把它放在反引号之间来告诉 MySQL 这是一个名字而不是 [=13= 中使用的 desc
]:
$query= mysqli_query($connect,
"INSERT INTO products(`name`,`desc`,`refno`,`quantity`)
VALUES('$names','$productdesc', '$refno','$quanitity')");
根据您的情况,始终在列或 table 名称周围加上反引号可能是个好主意,以确保即使某个词突然变成保留字,您的代码也不会中断.
desc
是保留字。如果您可以控制架构,我建议您将列重命名为非保留字,例如 description
。如果这不可能或不可行,您可以通过用反引号包围它来转义列名:
$query= mysqli_query($connect,
"INSERT INTO products(name,`desc`,refno,quantity)
VALUES('$names','$productdesc', '$refno','$quanitity')");
旁注:
在 SQL 语句中使用字符串操作会使您的代码容易受到 SQL 注入攻击。您应该考虑改用 prepared statements。
我正在尝试通过提交表单来插入数据,这是我收到的错误
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 'desc,refno,quantity,imgname, image)
VALUES('name','Some desc ', '123','123' at line 1
以下是我的 sql 查询
$query= mysqli_query($connect,
"INSERT INTO products(name,desc,refno,quantity)
VALUES('$names','$productdesc', '$refno','$quanitity')");
以下是数据库的附加屏幕截图
如果我调用 print_r
这就是我得到的结果
Array
(
[name] => name
[refno] => 123
[quantity] => 123456
[description] => Some desc
[submit] => Add Product
)
因为 desc
是一个保留字,你需要把它放在反引号之间来告诉 MySQL 这是一个名字而不是 [=13= 中使用的 desc
]:
$query= mysqli_query($connect,
"INSERT INTO products(`name`,`desc`,`refno`,`quantity`)
VALUES('$names','$productdesc', '$refno','$quanitity')");
根据您的情况,始终在列或 table 名称周围加上反引号可能是个好主意,以确保即使某个词突然变成保留字,您的代码也不会中断.
desc
是保留字。如果您可以控制架构,我建议您将列重命名为非保留字,例如 description
。如果这不可能或不可行,您可以通过用反引号包围它来转义列名:
$query= mysqli_query($connect,
"INSERT INTO products(name,`desc`,refno,quantity)
VALUES('$names','$productdesc', '$refno','$quanitity')");
旁注:
在 SQL 语句中使用字符串操作会使您的代码容易受到 SQL 注入攻击。您应该考虑改用 prepared statements。