MySQL: “You have an error in your SQL syntax… near 'desc) VALUES ('Idea','Description')'” [duplicate]

狂风中的少年 提交于 2019-12-07 05:28:39

问题


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:

"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"

I am running a .php file from a webserver to execute this script.

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.


回答1:


It may be because desc is a keyword in SQL. Try a different name. desc is used to sort results in descending order.

In general I would recommend to avoid to use reserved words for column names.




回答2:


desc is a reserved keyword (short for DESCENDING in ORDER BY).

Enlose it into backticks:

INSERT INTO ideas (title, `desc`) VALUES ('$title','$desc');


来源:https://stackoverflow.com/questions/5357903/mysql-you-have-an-error-in-your-sql-syntax-near-desc-values-idea-des

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!