This simple foreach loop was working but now it's not and I can't see anything wrong. $FieldNames is an array of column names from the database table and $row is an array containing the values if it's an existing entry. It creates the variable names from the column names using variable variables, then populates them if there is an open record. If no record is open, then there are no values for $row. These values are ultimately used on a form but the form is not currently being populated when an existing entry needs to be updated.
The only change I know of is that this development PC recently updated to PHP 8 but otherwise the programming itself hasn't been touched. Any ideas?
// Create variable variables from table column names and populate on post or from existing entry
foreach ($FieldNames as $row=>$val) :
$$val = (isset($_POST[$val])) ? safeData($_POST[$val]) : (isset($row[$val]) ? addslashes($row[$val]) : '');
endforeach;
Sample of $row:
(
[ID] => 1
[FirstName] => John
[LastName] => Doe
[UserLogin] => JohnD
[UserPassword] => MyPassword
[Email] => [email protected]
)
Sample of $FieldNames:
(
[0] => ID
[1] => FirstName
[2] => LastName
[3] => UserLogin
[4] => UserPassword
[5] => Email
)
$FieldNames should be providing the variable variables regardless of whether $row has any data and until PHP8 it was working.
I tried simplifying to this $$val = (isset($_POST[$val])) ? safeData($_POST[$val]) : safeData($row[$val]); and it gives values when the form has an entry but when empty, each missing variable gives an error.