PDO UPDATE:语法错误或访问冲突

时间:2015-11-08 09:42:41

标签: php mysql sql pdo

我是PDO的新手,撰写更新查询:

$sql = "UPDATE `users` 
           SET(`uname` = :uname,
               `role` = :role,
               `fname` = :fname,
               `email` = :email,
               `mobile1` = :mobile1,
               `mobile2` = :mobile2,
               `education` = :education,
               `division` = :division,
               `district` = :district,
               `sub_district` = :sub_district,
               `address` = :address,
               `looking_for` = :looking) 
         WHERE `id` = :id";

            //$sql = "UPDATE `users` SET(`uname`=?,`role`=?,`fname`=?,`email`=?,`mobile1`=?,`mobile2`=?,`education`=?,`division`=?,`district`=?,`sub_district`=?,`address`=?,`looking_for`=?) WHERE `id`=?";
            $st = $conn->prepare($sql);
            //$res['sql'] = $st->queryString;
            $params = array(
                ':uname' => $uname, 
                ':role' => $role, 
                ':fname' => $fname, 
                ':email' => $email, 
                ':mobile1' => $mobile1, 
                ':mobile2' => $mobile2, 
                ':education' => $edu, 
                ':division' => $division, 
                ':district' => $district, 
                ':sub_district' => $sub_district, 
                ':address' => $address, 
                ':looking' => $looking, 
                ':id' => $id
            );

            //$res['params'] = $params;
            $r = $st->execute($params);     

得到:

  

SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MariaDB服务器版本,用于在'(uname =附近使用正确的语法   ' rahman@abc.def',role =' 2',fname ='全名',email =' rahm'   在第1行

有人能告诉我代码中的错误吗?

这是我的表结构:

enter image description here

1 个答案:

答案 0 :(得分:10)

删除SET部分:

周围的括号
$sql  =  "UPDATE `users` 
          SET  `uname` = :uname,
               `role` = :role,
               `fname` = :fname,
               `email` = :email,
               `mobile1` = :mobile1,
               `mobile2` = :mobile2,
               `education` = :education,
               `division` = :division,
               `district` = :district,
               `sub_district` = :sub_district,
               `address` = :address,
               `looking_for` = :looking 
         WHERE `id` = :id";

检查UPDATE syntax

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]