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


1 answer


Assuming what you mean COALESCE

, replace FirstName with a space (''), then try using NULLIF

:

UPDATE `Users` 
SET FirstName = COALESCE(NULLIF(:firstName,''), FirstName) ...

      



I am assuming the POST is sending a space.

Good luck.

+5


source







All Articles