簡體   English   中英

PHP PDO Update准備的語句問題

[英]PHP PDO Update prepared statement problem

大家好,我最近一直在嘗試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服務器版本相對應的手冊,以在'(Username ='test',Password ='unknown',Email ='email')附近使用正確的語法WHERE Username ='use'在第1行

任何幫助將非常感激。

UPDATE查詢的SET子句中沒有括號。

http://dev.mysql.com/doc/refman/5.0/en/update.html

因此,當(被點擊時,語法錯誤。只要您嘗試使用綁定參數正確執行操作,也可以在WHERE子句中執行操作!

  • 您的WHERE部分未使用准備好的語句

  • 使用die()不是路要走; 不要使用OR,而是檢查引發的異常。

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

  • 看來您的PDO處於兼容模式。 最好將其關閉

  • 我想查看最終查詢,通常有很大幫助

  • 這是我關於同一主題的問題,希望您能發現它有用:
    使用PDO插入/更新幫助器功能

  • 但是,我將PDO放在一邊,然后轉回適合我的舊mysql。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM