data as $name => $value) { if (empty($value) and '0' != $value) { $form->data[$name] = NULL; } }Please advise~ Thanks a lot!"> Update DB for empty fields - Forums

Forums

Update DB for empty fields

zigzo 24 Jun, 2014
Hello,
I have an "edit existing record" form which can show the existing field data and update the record if user changes the field data.
However, if user changes the fields to empty, or uncheck some check-boxes, the fields in the existing record will not be changed. I hope the fields can be changed to NULL instead.

I tried setting the below before DB save but it doesn't work:
foreach ($form->data as $name => $value) {
        if (empty($value) and '0' != $value) {
            $form->data[$name] = NULL;
        }
    }


Please advise~ Thanks a lot!
GreyHead 24 Jun, 2014
Hi zigzo,

I think that setting them to a PHP NULL effectvely removes them from the $form->data[] array and so they are not updated. Try setting them empty instead $form->data[$name] = ''; that way they will be saved but with an empty value.

I don't know how you can set a MySQL NULL using this method though.

Bob
zigzo 26 Jun, 2014
Thanks for the reply, Bob.
If I change the fields to '', the decimal fields will be updated to 0.00 which is not what I want.
I would probably try writing the update query to set the fields to null.
GreyHead 26 Jun, 2014
Hi zigzo,

Please let us know how you get on. I'm pretty certain that a hand-coded save query will work; I'm not at all clear how to avoid quoting the NULL entry* if you use the CF or Joomla! save methods :-(

Bob

* i.e. making it 'NULL' in place of NULL.
zigzo 27 Jun, 2014
Hi Bob,

I have given up writing my own query in the end... It is easier for me to change my default fields to a certain value. e.g. -9999. Thanks anyway!
This topic is locked and no more replies can be posted.