rul-6ren">
gpt4 book ai didi

php - 未捕获的异常 'PDOException',消息为 'SQLSTATE[HY093]: Invalid parameter number'

转载 作者:行者123 更新时间:2023-11-29 11:32:30 29 4
gpt4 key购买 nike

每当我尝试使用 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);
}

保存函数生成查询字符串和绑定(bind),它们看起来都是正确的。

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"
}

每当我将查询放入工作台时,我都没有问题,但是当尝试在代码中运行它时,我得到 fatal error :未捕获的异常“PDOException”,带有消息“SQLSTATE[HY093]:无效的参数号”,这让我很困惑,因为绑定(bind)参数的数量与绑定(bind)键和数字匹配。谁能帮我解答一下这个问题?

最佳答案

我认为这可能是因为您在声明中将每个绑定(bind)声明了两次,例如:firstname 出现在 VALUES 子句以及 ON DUPLICATE KEY UPDATE 子句中。

您只将 8 个绑定(bind)传递给 $stmt->execute,但 PDO 正在寻找 16 个。

您可以尝试在 ON DUPLICATE KEY UPDATE 子句中对它们进行稍微不同的命名,为您提供一个查询,例如:


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名字 = :update_firstName,姓氏 = :update_lastName,用户名 = :update_username,密码 = :update_password,电子邮件 = :update_email,isSuperUser = :update_isSuperUser,dateCreated = :update_dateCreated,dateLastModified = :update_dateLastModified;

关于php - 未捕获的异常 'PDOException',消息为 'SQLSTATE[HY093]: Invalid parameter number',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37173386/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com