php - 使用 PHP PDO 插入多行

标签 php mysql pdo

我正在尝试使用 PHP PDO 将两行插入到 MySQL 表中。下面是我的代码

$bundleParams[] = array(
     ':bundle_contract_id' => $contract_id,
     ':bundle_clin' => $clin,
     ':constituent_clin' => $constituent_clin,
     ':constituent_quantity' => $constituent_quantity,
     ':constituent_price' => $constituent_price,
     ':base_clin' => 'Y'
);
$bundleParams[] = array(
     ':bundle_contract_id' => $contract_id,
     ':bundle_clin' => $clin,
     ':constituent_clin' => 'DELL-COMPONENTS',
     ':constituent_quantity' => $constituent_quantity,
     ':constituent_price' => 0.00,
     ':base_clin' => 'N'
);
$insertSQL = "INSERT INTO product_bundle(bundle_contract_id, bundle_clin, constituent_clin, constituent_quantity, constituent_price, base_clin) VALUES (:bundle_contract_id, :bundle_clin, :constituent_clin, :constituent_quantity, :constituent_price, :base_clin)";
$stmt = $pdo->prepare($insertSQL);
$stmt->execute($bundleParams);

当我执行代码时,出现错误

Fatal error: Uncaught exception 'PDOException'

有消息

SQLSTATE[HY093]: Invalid parameter number

这段代码有什么问题?谁能帮我跟踪这个问题吗?非常感谢您抽出宝贵的时间。

最佳答案

$stmt->execute($array); 想要一个与 PDO 绑定(bind)的关联数组,你给出一个这样的数组的数组 -> 你必须为每个数组执行此操作(如你的常识已经说过了):

foreach ($bundleParams as $row)
    $stmt->execute($row);
}

关于php - 使用 PHP PDO 插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141378/

相关文章:

php - 如何在函数中使用外部变量?

PHP PDO 包装器更新 +1

php - fatal error : Call to a member function rowCount() on a non-object

php - 使用PDO提取单行,单列

php - PHP 网站的快速图书集成

PHP如何获取基本域/url?

php mysql 多次更新什么也不做

mysql - 远程 MySQL 连接请求被拒绝

php - SQL 查询在通过 phpmyadmin 测试时工作正常但在 PHP PDO 中尝试相同的事情时失败

php - exec中的ffmpeg PHP变量不起作用