Saturday, 31 August 2013

PHP using foreach to insert multiple rows not working

PHP using foreach to insert multiple rows not working

I'm trying to check if a row exists for a specific transaction and when a
new one is submitted it inserts, however, it's only inserting the first
one and not the next 5 that are there too.
Code:
foreach($transactions as $transaction) {
$check_if_transaction_exists =
$db->query("SELECT * FROM
`transactions`")->fetchColumn();
if($check_if_transaction_exists == 0) {
$insert_transaction =
$db->prepare("INSERT INTO
`transactions`
(`deposit_address`,
`amount`,
`timereceived`,
`txid`,
`category`)
VALUES
(:deposit_address,
:amount,
:timereceived,
:txid,
:category)");
$insert_transaction->execute(array(
':deposit_address' =>
$_SESSION['deposit_address'],
':amount' =>
$transaction['amount'],
':timereceived' =>
$transaction['timereceived'],
':txid' =>
$transaction['txid'],
':category' =>
$transaction['category']
));
}
echo '<br />';
echo '<br />' . $transaction['address'];
echo '<br />' . $transaction['amount'];
echo '<br />' . $transaction['confirmations'];
date_default_timezone_set('America/Los_Angeles');
echo '<br />'; echo date("F j, Y, h:i A,",
$transaction['timereceived']);
}
The bottom bit of code returns 6 of the designated elements (address,
amount, confs, etc. ) but only inserts the very first one into the
database. How can I make it insert all of them?

No comments:

Post a Comment