我已经阅读了很多讨论这个问题的帖子,我无法找到我的代码有什么问题。我有一个数组:
$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";
稍后,我有:
$q_customer = 'insert into customers
(
cpq_id,
shopping_cart_id,
user_id,
company_name,
crm_id
)
values (?, ?, ?, ?, ?)';
$sc = $db2->prepare($q_customer);
后来还是:
foreach ($insert_arr as $id => $arr) {
foreach($arr['customer'] as $c) {
$sc->execute($c);
}
}
我总是得到标题中引用的错误。我之前从未使用过PDO,但我无法弄清楚错误是什么。求救!
答案 0 :(得分:1)
您必须先绑定参数才能调用execute方法。您的数组也是第三维类型,因此您需要添加foreach。
像这样:
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();
答案 1 :(得分:1)
如果要使用关联数组作为execute()
的参数,则必须使用命名占位符,而不是?
。占位符名称必须与数组索引匹配。
$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);
}
}