2011-08-25 102 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