問題描述
每當我嘗試使用 PDO 插入我的數據庫時,我都會收到錯誤消息.
I'm receiving an error whenever I attempt to insert into my database using PDO.
public function save($primaryKey = "") {
$validate = $this->rules();
if ($validate === true) {
$properties = '';
$values = '';
$bindings = array();
$update = '';
foreach ($this as $property => $value){
if ($property === "conn") {
continue;
}
$properties .= $property . ',';
$values .= ':' . $property . ',';
$update .= $property . ' = :' . $property . ',';
$bindings[':'.$property] = $value;
}
$sql_string = 'INSERT INTO ' . get_class($this) . ' (' . rtrim($properties, ',') . ') ';
$sql_string .= 'VALUES (' . rtrim($values, ',') . ') ON DUPLICATE KEY UPDATE ' . rtrim($update, ',') . ';';
$result = $this->executeQuery(NULL, $sql_string, $bindings);
$this->buildObject($result);
if (!empty($primaryKey)) {
$this->$primaryKey = $this->conn->lastInsertId();
}
return $result;
} else {
return $validate;
}
}
public function executeQuery($object, $sql_string, $bindings = null) {
$stmt = $this->conn->prepare($sql_string);
if (!empty($bindings)) {
if (!$stmt->execute($bindings)) {return false;}
} else {
if (!$stmt->execute()) {return false;}
}
$result = (!empty($object) ? $stmt->fetchAll(PDO::FETCH_CLASS, $object) : $stmt->fetchAll());
return (($stmt->rowCount() > 0) ? $result : false);
}
save 函數生成查詢字符串和綁定,兩者看起來都是正確的.
The save function generates both the query string and the bindings which both seem correct.
query = INSERT INTO am_administrator (firstName,lastName,username,password,email,isSuperUser,dateCreated,dateLastModified) VALUES (:firstName,:lastName,:username,:password,:email,:isSuperUser,:dateCreated,:dateLastModified) ON DUPLICATE KEY UPDATE firstName = :firstName,lastName = :lastName,username = :username,password = :password,email = :email,isSuperUser = :isSuperUser,dateCreated = :dateCreated,dateLastModified = :dateLastModified;
bindings = array(8) {
[":firstName"]=> string(5) "First"
[":lastName"]=> string(4) "Last"
[":username"]=> string(7) "cova-fl"
[":password"]=> string(8) "password"
[":email"]=> string(16) "test@testing.com"
[":isSuperUser"]=> int(1) "1"
[":dateCreated"]=> string(19) "2016-05-11 02:40:15"
[":dateLastModified"]=> string(19) "2016-05-11 02:40:15"
}
每當我將查詢放入工作臺時,我都沒有問題,但是當我嘗試在代碼中運行它時,我收到致命錯誤:未捕獲的異常 'PDOException',消息為 'SQLSTATE[HY093]:無效的參數編號',這讓我感到困惑,因為綁定參數的數量與綁定鍵和數字匹配.任何人都可以在這個問題上啟發我嗎?
Whenever I put the query into workbench I have no problems, but when trying to run it in code I get Fatal Error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number' which confuses me since the number of bindings params matches the bindings keys and nummbers. Can anyone enlighten me on this issue?
推薦答案
我認為這可能是因為您在語句中對每個綁定進行了兩次 decared,例如:firstname
出現在 VALUES
子句以及 ON DUPLICATE KEY UPDATE
子句中.
I think this might be because you have decared each binding twice in the statement e.g. :firstname
appears in the VALUES
clause as well as the ON DUPLICATE KEY UPDATE
clause.
您只向 $stmt->execute
傳遞了 8 個綁定,但 PDO 正在尋找 16 個.
You only pass 8 bindings to the $stmt->execute
but PDO is looking for 16.
您可以嘗試在 ON DUPLICATE KEY UPDATE
子句中對它們的命名略有不同,為您提供一個查詢,例如
You could try naming them slightly different in the ON DUPLICATE KEY UPDATE
clause giving you a query such as e.g.
<代碼>INSERT INTO am_administrator (firstName,lastName,username,password,email,isSuperUser,dateCreated,dateLastModified) VALUES (:firstName,:lastName,:username,:password,:email,:isSuperUser,:dateCreated,:dateLastModified) ON DUPLICATE KEY UPDATEfirstName = :update_firstName,lastName = :update_lastName,username = :update_username,password = :update_password,email = :update_email,isSuperUser = :update_isSuperUser,dateCreated = :update_dateCreated,dateLastModified = :update_dateLastModified;
這篇關于未捕獲的異常 'PDOException' 帶有消息 'SQLSTATE[HY093]: Invalid parameter number'的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!