I am having trouble updating an array of data in nested foreach loops. I have read many similar issues on SO but non using a nested foreach (see my array schema) and the answer seems to be using the 'by reference' operator '&' on the value - but this is not working for me. Ive tried using the 'by ref' operator on the outer loop var too since it kind of seems logical but to no avail. Here's my code and array dumps :
The input array schema:
array (
0 =>
array (
'ReviewDate' => '2016-05-08 00:00:00',
'ReviewDetails' => 'Release pending',
'RentAmount' => '112000.00',
'ReviewComplete' => 1,
),
1 =>
array (
'ReviewDate' => '2022-10-25 00:00:00',
'ReviewDetails' => 'Short-hold still open',
'RentAmount' => '21000.00',
'ReviewComplete' => 0,
),
)
This is my prepared statement:
'UPDATE rentals SET `ReviewDate`=:ReviewDate, `ReviewDetails`=:ReviewDetails, `RentAmount`=:RentAmount, `ReviewComplete`=:ReviewComplete WHERE agreemID=14'
My code is:
try
{
$cnx->beginTransaction();
$stmt = $dbconx->prepare( $QryStr );
foreach ( $formArr as $tbl => $rec )
{
foreach ( $rec as $key => &$val ) {
$stmt->bindParam( $key, $val );
}
}
$stmt->execute();
$cnx->commit();
return $stmt->rowCount();
}
catch ( PDOException $e )
{
$cnx->rollBack();
$this->applog->logerr( __FUNCTION__ ." - ".$e->getMessage() );
return false;
}
The DB table is update as follows - as you can see records are being overwritten with the final statement:
array (
0 =>
array (
'ReviewDate' => '2022-10-25 00:00:00',
'ReviewDetails' => 'Short-hold still open',
'RentAmount' => '21000.00',
'ReviewComplete' => 0,
),
1 =>
array (
'ReviewDate' => '2022-10-25 00:00:00',
'ReviewDetails' => 'Short-hold still open',
'RentAmount' => '21000.00',
'ReviewComplete' => 0,
),
)
This is precisely what i was getting before using the '&' by reference char - but its not working for me and i'm getting no errors in my log file. My connection is good as i'm using it everywhere else. Do i have to write out each binding separately? I'm trying to avoid doing that because i have over 30 similar tables to update. Any help appreciated