I am attempting to create a form that, when submitted, will update the values that were inserted into the form and leave the unanswered values unchanged.
My idea was to create a loop that would set all the $_POST keys and values into an update statement. The names of each key correspond with each column in my table and so it should work out.
This is what I came up with:
$query = "UPDATE accounts SET ";
foreach($_POST as $field => $value) {
if ($field != null && $value != 'Update Information!'){
$query .= "{$field} = {$value}";
$query .= ", ";
}
}
$query .= "WHERE id = {$current_user["id"]}";
The issue I am running into is the last line of the loop. The loop inserts a comma at the end of each loop which is fine until the last value where it messes up the UPDATE statement.
Is there anyway to exclude the comma on the last loop? Thanks!