MYSQL查询错误WHERE id = $ id

时间:2012-12-06 03:34:00

标签: php mysql sql syntax

我正在创建然后在表中编辑一行,但是我在php中的编辑mysql查询给了我一个我无法弄清楚的错误。有什么帮助吗?

创建查询:

$query = "INSERT INTO timelines (
id, event_name, event_date, date_created, attendee_count, attendee_names, maximum_attendees, creator_id, creator_name, price, thumbnail
) VALUES (
'{$timelineID}', '{$event_name}', '{$event_date}', '{$date_created}', '{$attendee_count}', '{$attendee_names}', '{$maximum_attendees}', '{$creator_id}', '{$creator_name}', '{$price}', '{$thumbnail}'
)";

编辑查询:

$query = "UPDATE timelines SET 
event_name = '{$event_name}', 
event_date = '{$event_date}', 
maximum_attendees = '{$maximum_attendees}', 
price = '{$price}', 
thumbnail = '{$thumbnail}',
WHERE id = {$timelineID}";

错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在第8行'WHERE id ='附近

1 个答案:

答案 0 :(得分:10)

WHERE子句之前有一个额外的逗号。只需删除它就能正常工作。

thumbnail = '{$thumbnail}',
                          ^ here
WHERE ...

最终查询,

$query = "UPDATE timelines SET 
event_name = '{$event_name}', 
event_date = '{$event_date}', 
maximum_attendees = '{$maximum_attendees}', 
price = '{$price}', 
thumbnail = '{$thumbnail}'
WHERE id = {$timelineID}";

您的查询容易被SQL INJECTION攻击,请阅读以下文章,了解如何保护它。