MySQL update query, how to skip empty values?

Posted by Pawel on Stack Overflow See other posts from Stack Overflow or by Pawel
Published on 2012-12-12T16:54:38Z Indexed on 2012/12/12 17:03 UTC
Read the original article Hit count: 163

Filed under:
|

I've got such query:

$sql = "UPDATE test_accs SET 
    acc_owner = '$owner_id', 
    acc_policy_version = '$version', 
    acc_policy_last_update = '$approved', 
    acc_policy_next_update = '$renewed' 
        WHERE acc_id = '1'";

Now, all of these values on the web folmular are optional, one can set one of these values, two, or so. Now, after I submit the form, it goes in the query like that:

UPDATE test_accs SET acc_owner = '2', acc_policy_version = '1.2', acc_policy_last_update = '2012-12-19', acc_policy_next_update = '2012-12-18' WHERE acc_id = '1'

It works only when I submit all values from the form. Can you please show me how could it work even if not all the values has been sent, just for example one of them?

When I set one value (f.ex. policy version), it looks like that:

UPDATE test_accs SET acc_owner = '', acc_policy_version = '1.2', acc_policy_last_update = '', acc_policy_next_update = '' WHERE acc_id = '1'

and it isn't working.

It might be possible cause of the acc_owner table values?

#1366 - Incorrect integer value: '' for column 'acc_owner' at row 1

Thanks in advice.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql