CodeJr
CodeJr

Reputation: 47

sql query update only one field

I'm building a PHP and sql API and I'm trying to create the update query:

$sql = "UPDATE boxes 
    SET box_id = :box_id, channel = :channel, status = :status 
    WHERE id = $id";

The problem is that if I just update the channel, for example, the other fields became empty in my database.

I want the user to be able to choose if he wants to update all fields, just some or only one.

Thanks

Upvotes: 2

Views: 376

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269543

Are you looking for coalesce()?

UPDATE boxes
    SET box_id = COALESCE(:box_id, box_id),
        channel = COALESCE(:channel, channel),
        status = COALESCE(:status, status)
    WHERE id = :id;

Notice that I made :id a named parameter as well.

Upvotes: 2

Related Questions