SQLSTATE[HY093]:绑定变量的数量与使用复合密钥更新复制密钥时的令牌数量不匹配



我读了很多关于类似问题的帖子,但仍然很困惑。。

该表有一个复合键idx_aq_contract_estimate(vendorId,projectId(。

我有9个占位符,$values数组中有9个值,array_map中的9个键映射到?用于ON DUPLICATE KEY UPDATE数组。

$connection = get_connection();
if(!empty($values)  && !empty($table)) {
$columnString = implode(', ', array_keys($values));
$arrayOnDuplicateKeyVal = implode(', ',array_map('getKeyValue',array_keys($values, )));
$valueString = implode(',', array_fill(0, count($values), '?'));
$statement = $connection->prepare("INSERT INTO $table ({$columnString}) VALUES ({$valueString}) ON DUPLICATE KEY UPDATE $arrayOnDuplicateKeyVal");
//print_r ($statement) at this point:
//INSERT INTO `aq_contract_estimate` (vendorId, name, projectId, exportId, total_freight, total_taxable, total_nontaxable, total_credit_taxable, total_credit_nontaxable) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE vendorId = ?, name = ?, projectId = ?, exportId = ?, total_freight = ?, total_taxable = ?, total_nontaxable = ?, total_credit_taxable = ?, total_credit_nontaxable = ?
)
$statement->execute(array_values($values));
print $statement->errorCode();
//prints: Invalid parameter number: number of bound variables does not match number of tokens upon $statement->execute()

print_r($statement->errorInfo());
$statement->closeCursor();

如果我删除";关于重复密钥更新$arrayOnDuplicateKeyVal"("该语句执行良好,所以我相信我的字段名拼写正确,等等,但我需要处理重复的键。

这种使用代币与指定位置持有者的方法有什么不正确的地方??

您可以将查询重写为

INSERT INTO `aq_contract_estimate` (vendorId, name, projectId, exportId, total_freight, total_taxable, total_nontaxable, total_credit_taxable, total_credit_nontaxable) 
VALUES (?,?,?,?,?,?,?,?,?) 
ON DUPLICATE KEY UPDATE vendorId = VALUES(vendorId), name = VALUES(name), projectId = VALUES(projectId)
, exportId = VALUES(exportId), total_freight = VALUES(total_freight), total_taxable = VALUES(total_taxable), total_nontaxable = VALUES(total_nontaxable), total_credit_taxable = VALUES(total_credit_taxable )
, total_credit_nontaxable = VALUES(total_credit_nontaxable)

因此,你不需要将价值加倍。

但是,当插入遇到唯一约束并违反该约束时,ON DUPICATE UPDATE是有效的,因此您实际上不需要更新一列或多列。

最新更新