2011-08-25 89 views
4

我使用PHP PDO將數據插入到它看起來像這樣我的數據庫表(稱爲stb_committee_members):PDO錯誤(42000)

Field  | Type 
-------------------- 
id   | int (10) (unsigned) 
stb_group_id | int (10) (unsigned) 
name   | varchar(50) (can be null, default NULL) 
position  | varchar(50) (can be null, default NULL) 
from   | varchar(50) (can be null, default NULL) 
experience | varchar(50) (can be null, default NULL) 
photo  | varchar(50) (can be null, default NULL) 

,這裏是我使用插入代碼數據庫:

$group_id = $dbh->lastInsertId(); 

    foreach ($committee_members as $committee_member) { 
     $com_member_name = $committee_member['name']; 
     $com_member_position = $committee_member['position']; 
     $com_member_from = $committee_member['from']; 
     $com_member_experience = $committee_member['experience']; 
     $com_member_photo = $committee_member['photo']; 

     $sth = $dbh->prepare("INSERT INTO stb_committee_members (stb_group_id, name, position, from, experience, photo) VALUES (?, ?, ?, ?, ?, ?)"); 
     $sth->bindParam(1, $group_id); 
     $sth->bindParam(2, $com_member_name); 
     $sth->bindParam(3, $com_member_position); 
     $sth->bindParam(4, $com_member_from); 
     $sth->bindParam(5, $com_member_experience); 
     $sth->bindParam(6, $com_member_photo); 
     $sth->execute(); 
    } 

這是錯誤消息我得到:

array(3) { 
    [0] => string(5) "42000" 
    [1] => int(1064) 
    [2] => 
     string(226) "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 'from, experience, photo) VALUES ('37', 'gfdgdf', 'gfdg', 'gfdgfd', 'gfdggfd', '1' at line 1" 
} 

object(PDOStatement)#3 (1) { 
    ["queryString"] => 
    string(115) "INSERT INTO stb_committee_members (stb_group_id, name, position, from,  experience, photo) VALUES (?, ?, ?, ?, ?, ?)" 
} 

而且我嘗試後的值(以):

string(2) "37" 
string(6) "gfdgdf" 
string(4) "gfdg" 
string(6) "gfdgfd" 
string(7) "gfdggfd" 
string(20) "1314261618-acida.png" 

我只是看不到有什麼問題!我所有的其他插入工作正常,並以相同的方式創建...

回答

8

from是SQL中的保留keyword,您必須使用反引號引用它。

$sth = $dbh->prepare("INSERT INTO stb_committee_members (stb_group_id, name, position, `from`, experience, photo) VALUES (?, ?, ?, ?, ?, ?)"); 
+3

........我現在一點也不覺得傻。 – Becky