大家好我最近一直在嘗試我的PDO,我正在嘗試爲我正在開發的項目編寫一個基本的數據庫類。但是我遇到了一些問題,試圖編寫一個函數來執行使用預處理語句的更新查詢。PHP 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);
這將返回以下錯誤:
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42000]: Syntax error or access violation: 1064 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 '(Username = 'test', Password = 'unknown', Email = 'email') WHERE Username = 'use' at line 1
任何幫助將非常感激。
啊這樣一個愚蠢的錯誤,感謝您的幫助。 – trottski 2011-02-05 15:54:25