-2

I am getting the following error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

Here's the code:

$transactions_sql = "INSERT INTO transactions (usr, service, txn_id, orig_amount, currency, date, description, fee_amt, 
  fee_currency, fee_descr, fee_type, net_amt, status) VALUES ";
$transactions_sql_data = array_fill(0, count($transactions), "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$transactions_sql .= implode(",", $transactions_sql_data);
$stmt = $conn->prepare($transactions_sql);
$i = 1;
foreach ($transactions["data"] as $tr) {
  $stmt->bindValue($i++, $id, PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["service"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["id"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["amount"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["currency"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["created"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["description"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["fee_details"][0]["amount"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["fee_details"][0]["currency"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["fee_details"][0]["description"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["fee_details"][0]["type"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["net"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["status"], PDO::PARAM_STR);
}
$stmt->execute();

Doing var_dump($transactions_sql) prints "INSERT INTO balance_transactions (usr, service, txn_id, orig_amount, currency, date, description, fee_amt, fee_currency, fee_descr, fee_type, net_amt, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", which is correct (in terms of number of question marks - there will only be three arrays inside the $tr array).

Community
  • 1
  • 1
DemCodeLines
  • 1,870
  • 8
  • 41
  • 60

1 Answers1

0

Turns out I had to add a -1 to count($transactions) for it to work. Looks like it was adding 1 too many set of parameters.

Here's what worked: $transactions_sql_data = array_fill(0, count($transactions), "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

Thanks to everyone else for helping out!

DemCodeLines
  • 1,870
  • 8
  • 41
  • 60