2013-07-18 183 views
-1

我无法将视图字符串插入数据库。我不完全确定是什么导致它,但这是我的代码;插入PHP和MySQL错误

$taskupdate = $db_new->prepare("INSERT INTO clients_tasks_updates (tid, sid, update, date, hours) VALUES (:tid, :sid, :update, :date, :hours)"); 
$taskupdate->execute(array(
     ':tid' => '1', 
     ':sid' => '2', 
     ':update' => 'Here is an update', 
     ':date' => '01-01-2013', 
     ':hours' => '50' 
)); 

这是错误代码我得到:

Array ([0] => 42000 [1] => 1064 [2] => 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, date, hours) VALUES ('1', '2', 'Here is an update', '01-01-2013', '50')' at line 1) 

Task ID: 2 
Staff ID: Scott 
Update: Layout built for upcoming wordpress site. 
Date: 22-10-2012 
Hours: 6 

回答

2

UPDATE是SQL中reserved word。如果您真的想将其包含在查询中,则需要引用它。

INSERT INTO clients_tasks_updates (tid, sid, `update`, `date`, hours) VALUES (.... 

列和表可以用保留字命名;但是每当你引用它们时,你都需要将它们包装起来以逃避它们。

更好的方法是重命名列,以便它不使用保留字 - 从长远来看,它会让您的生活更轻松。