PHP PDO更新准备语句问题

PHP PDO更新准备语句问题,php,mysql,pdo,prepared-statement,Php,Mysql,Pdo,Prepared Statement,大家好,我最近一直在PDO上尝试,目前正在为我正在进行的一个项目编写一个基本的数据库类。然而,我在尝试编写一个函数来使用准备好的语句执行更新查询时遇到了问题 function update($tabledata, $table, $where){ $fields = array_keys($tabledata); $data = array_values($tabledata); $fieldcount = count($fields); $wherefield =

大家好,我最近一直在PDO上尝试,目前正在为我正在进行的一个项目编写一个基本的数据库类。然而,我在尝试编写一个函数来使用准备好的语句执行更新查询时遇到了问题

    function update($tabledata, $table, $where){

  $fields = array_keys($tabledata);
  $data = array_values($tabledata);
  $fieldcount = count($fields); 

  $wherefield = implode(array_keys($where));
  $whereval = implode(array_values($where));

  $this->query = "UPDATE $table SET ";
  $this->query .= '(' . implode($fields, ' = ?, ') . ' = ?)';
  $this->query .= " WHERE $wherefield = '$whereval'";

   $this->query = $this->_clean($this->query);
   $stmt = $this->conn->prepare($this->query) or die('Problem preparing query');
   $stmt->execute($data)or die('Problem executing query');

}
其用途的一个例子是:

 $usertbl = 'users';
 $date = date("Y-m-d");
 $updatedata = array(
   'Username' => 'test',
   'Password' => 'unknown',
   'Email' => 'email',
   );
$where = array(
   'Username' => 'user'
    );

$Database->update($updatedata,$usertbl,$where);
这将返回以下错误:

警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42000]:语法错误或 访问冲突:1064您有一个 SQL语法错误;检查 与您的MySQL相对应的手册 正确语法的服务器版本 使用near'(用户名='test',密码 ='unknown',Email='Email'),其中第1行的用户名='use'


任何帮助都将不胜感激。

查询的
SET
子句中没有括号

因此,当点击
)时会出现语法错误。只要您试图使用绑定参数以正确的方式进行操作,也可以在
WHERE
子句中进行操作!

  • 您的WHERE部分没有使用准备好的语句

  • 使用die()不是正确的方法;不要使用或,而是检查抛出的异常

  • 我很好奇,_clean()方法做什么

  • 您的PDO似乎处于兼容模式。最好将其关闭

  • 我想看看最后的查询,它通常帮助很大

  • 这是我关于同一主题的问题,我希望你能发现它有用:

  • 然而,我把PDO放在一边,转而使用更适合我的旧mysql