[英]SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'
I've read a bunch of threads discussing this problem and I can't find what's wrong with my code.I have an array: 我读过很多讨论此问题的线程,但我找不到我的代码有什么问题,我有一个数组:
$insert_arr["$key"]['customer']["$cust->Company"]['cpq_id'] = "$cust->CustomerId";
$insert_arr["$key"]['customer']["$cust->Company"]['shop_cart_id'] = "$cust->ShopCartId";
$insert_arr["$key"]['customer']["$cust->Company"]['user_id'] = "$cust->UserId";
$insert_arr["$key"]['customer']["$cust->Company"]['company'] = "$cust->Company";
$insert_arr["$key"]['customer']["$cust->Company"]['crm_id'] = empty("$cust->CRMAccountId") ? 0 : "$cust->CRMAccountId";
A bit later, I have: 稍后,我有:
$q_customer = 'insert into customers
(
cpq_id,
shopping_cart_id,
user_id,
company_name,
crm_id
)
values (?, ?, ?, ?, ?)';
$sc = $db2->prepare($q_customer);
And later still: 后来:
foreach ($insert_arr as $id => $arr) {
foreach($arr['customer'] as $c) {
$sc->execute($c);
}
}
And I always get the error referenced in the title. 而且我总是得到标题中引用的错误。 I've never used PDO before, and I can't figure out what's wrong.
我以前从未使用过PDO,也无法弄清楚出了什么问题。 Help please!
请帮助!
You have to bind your parameters before to call the execute method. 您必须先绑定参数,然后才能调用execute方法。 Your array is also a 3rd dimension type, so you will need to add a foreach.
您的数组也是第3维类型,因此您将需要添加一个foreach。
Like this : 像这样 :
foreach ($insert_arr as $id => $arr) {
foreach($arr['customer'] as $c) {
$params = array();
foreach($c as $param => $value) {
$params[] = $value;
}
$sc->execute($params);
}
}
$sc->execute();
If you want to use an associative array as the argument to execute()
, you have to use named placeholders, not ?
如果要使用关联数组作为
execute()
的参数,则必须使用命名的占位符,不是?
. 。 The placeholder names must match the array indexes.
占位符名称必须与数组索引匹配。
$q_customer = 'insert into customers (cpq_id, shopping_cart_id, user_id, company_name, crm_id) values (:cpq_id, :shop_cart_id, :user_id, :company, :crm_id)';
$sc = $db2->prepare($q_customer);
foreach ($insert_arr as $id => $arr) {
foreach($arr['customer'] as $c) {
$sc->execute($c);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.