du8589840
du8589840
2012-05-06 23:12

获取SQL语法错误

已采纳

I have a line of code in PHP as follows...

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

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 'update) VALUES ('14', '2012-05-06', 'Test update')' at line 1

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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douchao1957 douchao1957 9年前

    date and update are reserved words in 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).

    Also, a fairly minor point, if project_id is an integer typed field, pass it an integer, not a string. Like:

     INSERT INTO `updates` (project_id, `date`, `update`) VALUES ({$project}, '{$date}', '{$update}')
    
    点赞 评论 复制链接分享
  • dpnfjx755573 dpnfjx755573 9年前

    First and foremost Thing: you can not user mysql preserver word. 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

    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. i.e 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

    点赞 评论 复制链接分享
  • doulianxi0587 doulianxi0587 9年前

    update is a keyword in SQL, encapsulate your mysql fields in backticks.

    点赞 评论 复制链接分享
  • drctyr2869 drctyr2869 9年前

    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. We cannot use it directly in the query. We need to escape it.

    点赞 评论 复制链接分享