简体   繁体   English

取得SQL语法错误

[英]Getting an SQL syntax error

I have a line of code in PHP as follows... 我在PHP中有以下代码行...

mysql_query("INSERT INTO `updates` (project_id, date, update) VALUES ('{$project}', '{$date}', '{$update}')") or die(mysql_error());

However I'm getting the following SQL syntax error... 但是我收到以下SQL语法错误...

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update) VALUES ('14', '2012-05-06', 'Test update')' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'update)VALUES('14','2012-05-06','Test update')'附近使用正确的语法

If anyone could help me with this that would be great, perhaps it's obvious but I just can't see what's wrong here! 如果有人可以帮助我,这很不错,也许这很明显,但是我看不出这里有什么问题!

Change the query as below: 更改查询,如下所示:

mysql_query("INSERT INTO `updates` (`project_id`, `date`, `update`) VALUES ('{$project}', '{$date}', '{$update}')") or die(mysql_error());

This is because date and update are registered keywords in MySQL. 这是因为日期和更新是MySQL中的注册关键字。 We cannot use it directly in the query. 我们不能在查询中直接使用它。 We need to escape it. 我们需要逃避它。

date and update are reserved words in MySQL. dateupdate是MySQL中的保留字。

You can use: 您可以使用:

"INSERT INTO `updates` (project_id, `date`, `update`) VALUES ('{$project}', '{$date}', '{$update}')"

Though ideally you should never use a reserved word as an entity name. 尽管理想情况下,您永远不要使用保留字作为实体名称。 It offers no advantages, yet has a few minor disadvantages (for example, makes the SQL less portable). 它没有优势,但也有一些次要劣势(例如,使SQL的可移植性较差)。

Also, a fairly minor point, if project_id is an integer typed field, pass it an integer, not a string. 还有一点,如果project_id是整数类型的字段,则将其传递给整数而不是字符串。 Like: 喜欢:

 INSERT INTO `updates` (project_id, `date`, `update`) VALUES ({$project}, '{$date}', '{$update}')

update是SQL中的关键字,将您的mysql字段封装在反引号中。

First and foremost Thing: you can not user mysql preserver word. 第一要务:您不能使用mysql保留字。 When you use it, be ready to waste your hours in finding out error. 使用它时,请准备好浪费时间查找错误。 Here is the list of reserve words: DO NOT USE ANY AMONG IT http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html 这是保留字的列表:请勿在其中使用任何内容http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Second: Even if you want to dare to use preserved keyword. 第二:即使您想敢于使用保留关键字。 User table prefix or column prefix along with reserved keyword. 用户表前缀或列前缀以及保留关键字。

Third: When ever you perform the database operations along php either quote each and every parameter where required or just user simple one. 第三:无论何时,只要您沿着php执行数据库操作,要么在需要的地方引用每个参数,要么仅是用户简单的一个。 ie if you wish to quote db table columns than surround each column by quote 即,如果您希望引用数据库表列而不是用引号将每一列包围

"INSERT INTO `updates` (`project_id`, `date`, `update`) VALUES ('{$project}', '{$date}', '{$update}')"

and if you don't quote then quote none of them 如果您不报价,则不报价

"INSERT INTO updates (project_id, date, update) VALUES ('{$project}', '{$date}', '{$update}')"

Hope this would help you 希望这对您有帮助

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

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