仅当 $_POST[ value ] 不为空时,我才尝试更新表 Users 。如果它为空,则应该保留列中已有的值。
$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();
我为 FirstName 列尝试了 COALESCE,为 LastName 尝试了 ISNULL。COALESCE 用空白(NULL)替换我的值,这与我想要完成的完全相反,并且 ISNULL 似乎不起作用。