So I have a php function that updates 2 columns in a database. It looks like this:
$fields = array("firstname" => "Joe", "lastname" = "Dunno");
$stmt = $connection->prepare("UPDATE users SET firstname = :firstname, lastname = :lastname WHERE user_id = :user_id");
foreach ($fields as $key => $value)
{
$stmt->bindParam(":" . $key, $value);
}
$stmt->bindParam(":user_id", $user_id);
However when i execute the statement for some reason it likes to update firstname and lastname both to Dunno instead of Joe and Dunno.
I tried echo'ing the $key and $value and it prints out correctly.
For some weird reason if i use this for loop it works correctly.
for ($fieldsKeys = array_keys($fields), $x = 0; $x < count($fields); $x++)
{
$stmt->bindParam(":" . $fieldsKeys[$x], $fields[$fieldsKeys[$x]]);
}