我已经基于 fetch 函数制作了这个动态更新函数(同样的方法,只是另一个用于 SET 的数组)。
但它似乎有一个错误。
public function update($table, $columns = array(), array $criteria = null)
{
// The query base
$query = "UPDATE $table";
$query .= ' SET ' . implode(', ', array_map(function($column) {
return "$column = ?";
}, array_keys($columns)));
// Start checking
if ($criteria) {
$query .= ' WHERE ' . implode(' AND ', array_map(function($column) {
return "$column = ?";
}, array_keys($criteria)));
}
$update = $this->pdo->prepare($query) or die('An error has occurred with the following message:' . $query);
$update->execute(array_values($criteria));
}
这就是我得到的错误(第 179 行 = `$update->execute(array_values($columns, $criteria));):
它有什么问题?为什么我会收到这个错误?
编辑新错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in C:\xampp\htdocs\argonite\includes\class\MYSQL\Database.class.php:179 Stack trace: #0 C:\xampp\htdocs\argonite\includes\class\MYSQL\Database.class.php(179): PDOStatement->execute(Array) #1 C:\xampp\htdocs\argonite\index.php(8): Database->update('argonite_server...', Array, Array) #2 {main} thrown in C:\xampp\htdocs\argonite\includes\class\MYSQL\Database.class.php on line 179