This question already has an answer here:
这个问题在这里已有答案:
- Syntax error due to using a reserved word as a table or column name in MySQL 1 answer
- 由于在MySQL 1答案中使用保留字作为表或列名称而导致语法错误
I am trying to get MySQL to work for my form submissions. I am having a problem when I try to insert into a table.
When I put information into my form and click submit (in this example the information is "Idea" in one field and "Description" in the other) I get this response:
我试图让MySQL为我的表单提交工作。当我尝试插入表格时遇到问题。当我将信息放入我的表单并单击提交时(在此示例中,信息在一个字段中为“Idea”,在另一个字段中为“Description”)我得到此响应:
"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) VALUES ('Idea','Description')' at line 1"
“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在'desc附近使用正确的语法'VALUES('Idea','Description')'在第1行”
I am running a .php file from a webserver to execute this script.
我正在从Web服务器运行.php文件来执行此脚本。
Here is my current code:
这是我目前的代码:
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("date_ideas") or die(mysql_error());
$title=$_POST['title'];
$title=mysql_real_escape_string($title);
$desc=$_POST['desc'];
$desc=mysql_real_escape_string($desc);
$submit="INSERT INTO ideas (title, desc) VALUES ('$title','$desc');";
mysql_query($submit) or die(mysql_error());
echo ("Idea submitted. Click <a href='Webroot/submit.php'>here</a> to go back and post another idea.");
?>
If you call an echo of the variables used it succeeds at passing through the information, so that's not the problem.
如果你调用所使用的变量的回声,它会成功传递信息,所以这不是问题。
2 个解决方案
#1
4
It may be because desc
is a keyword in SQL. Try a different name. desc
is used to sort results in descending order.
这可能是因为desc是SQL中的关键字。尝试其他名称。 desc用于按降序对结果进行排序。
In general I would recommend to avoid to use reserved words for column names.
一般情况下,我建议避免对列名使用保留字。
#2
11
desc
is a reserved keyword (short for DESCENDING
in ORDER BY
).
desc是保留关键字(ORDER BY中DESCENDING的缩写)。
Enlose it into backticks:
将其加入反叛:
INSERT INTO ideas (title, `desc`) VALUES ('$title','$desc');
#1
4
It may be because desc
is a keyword in SQL. Try a different name. desc
is used to sort results in descending order.
这可能是因为desc是SQL中的关键字。尝试其他名称。 desc用于按降序对结果进行排序。
In general I would recommend to avoid to use reserved words for column names.
一般情况下,我建议避免对列名使用保留字。
#2
11
desc
is a reserved keyword (short for DESCENDING
in ORDER BY
).
desc是保留关键字(ORDER BY中DESCENDING的缩写)。
Enlose it into backticks:
将其加入反叛:
INSERT INTO ideas (title, `desc`) VALUES ('$title','$desc');