Adrian
Adrian

Reputation: 2656

Mysqli: skip update if variable is NULL

Is it possible to skip update if variable is NULL?

Query:

  $stmt = $mysqli->prepare("UPDATE personal SET name=?, gender=?, telp=?, address=?, date_deadline=? WHERE id_personal=?");
  $stmt->bind_param('ssssss', $nm, $gd, $tl, $ar, $dt, $id);

Here, I want to skip date_deadline field.

Upvotes: 0

Views: 399

Answers (2)

Script47
Script47

Reputation: 14540

From how you've explained your question the answer seems very simple, try the following:

if (is_null($myVar)) {
    // Code to run if null.
} else {
    // Update query here.
}

Edit 1:

If you don't want to run any code if it is null. Then do the following:

if (!is_null($myVar)) {
    // Code to run if not null.
}

Upvotes: 1

juergen d
juergen d

Reputation: 204766

UPDATE personal 
SET name=?, gender=?, telp=?, address=?,
    date_deadline = case when ? is null 
                         then date_deadline
                         else ? 
                    end
WHERE id_personal = ?

$stmt->bind_param('sssssss', $nm, $gd, $tl, $ar, $dt, $dt, $id);

Upvotes: 1

Related Questions