0

I'm trying to insert two rows to MySQL table using PHP PDO. Below is my code

$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);

When I execute the code I get the error

Fatal error: Uncaught exception 'PDOException'

with message

SQLSTATE[HY093]: Invalid parameter number

What's wrong with this code?. Can anyone help me track the issue? Thank you very much for your time.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Bineesh Varghese
  • 105
  • 3
  • 15

2 Answers2

0
$stmt = $pdo->prepare($insertSQL);
foreach ($bundleParams as $row)
    $stmt->execute($row);
}

All necessary explanations can be found in the linked answer.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

$stmt->execute($array); wants an associative array with the PDO bindings, you give a array of such arrays -> you have to do it for each of those so (as Your Common Sense already said) :

foreach ($bundleParams as $row)
    $stmt->execute($row);
}
inetphantom
  • 2,498
  • 4
  • 38
  • 61