SQL更新未运行,原因未知

时间:2014-08-29 23:10:47

标签: sql

我正在尝试运行更新,但由于原因我无法弄清楚它为什么没有运行。

错误:

Failed to run query: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'WHERE EventID = '2'' at line 4

我无法弄清楚语法有什么问题。

 $query = " 
      UPDATE event 
      SET AssignedTo = '$AssignedTo',Project = '$Project',Category = '$EventCategory',
      Status = '$Status',Services = '$EventServices',Priority = '$EventPriority',
      WHERE EventID = '$ID' ";


    try {  
        $stmt = $db->prepare($query); 
       $stmt->execute(); 


    } 
    catch(PDOException $ex){ die( "Failed to run query: " . $ex->getMessage()); } 

    die("                      Changes Submitted"); 
} 

1 个答案:

答案 0 :(得分:2)

这需要格式化文本,因此评论时间过长。

如果您整齐地格式化查询,那么您可以避免或至少最小化此类问题:

UPDATE event 
      SET AssignedTo = '$AssignedTo',
          Project = '$Project',
          Category = '$EventCategory',
          Status = '$Status',
          Services = '$EventServices',
          Priority = '$EventPriority',
-------------------------------------^
      WHERE EventID = '$ID';

查询的行不会滚动页面,从而更容易发现额外的逗号。