Update database if Post is not null
I am trying to update the Users table only if $ _POST [value] is not null. If it is null, the value that is already in the column should remain.
$query = "UPDATE `Users`
SET FirstName = COALESCE(:firstName, FirstName), LastName = ISNULL(:lastName, LastName), City = :city, State = :state WHERE Email = :email";
$stmt = $dbh->prepare($query);
$stmt->bindValue(':firstName', $firstName);
$stmt->bindValue(':lastName', $lastName);
$stmt->bindValue(':city', $city);
$stmt->bindValue(':state', $state);
$stmt->bindValue(':email', $email);
$stmt->execute();
I tried COALESCE for FirstName and ISNULL for LastName. COALESCE replaces my value with NULL, which is exactly what I want to accomplish and ISNULL doesn't work.
+3
source to share