Forums

Save to database with null value

mowe_a 14 Feb, 2025

Hi,

my database table has a column "size" of type integer (nullable). 

When I save into this table from chronoforms and the optional field "size" is filled with an integer, everything works fine. If the field "size" is left blank, the database won't accept the empty string for this column. To handle this, I added a PHP action before the save_data:

if (empty($this->data['size'])) {
    $this->data['size'] = null;
}

In the debugger, I can see the result, as in the data-Array: [size] => NULL

But the SQL-Update is nevertheless using an empty string instead of the NULL-Value:

[sql] => UPDATE `product` SET `product` = 'Test Product', `size` = '', `price` = '1.00' WHERE product_id='427090'

How can I handle it to write Null-Values to the DB under some conditions?

Max_admin 14 Feb, 2025
Answer

to save null you need to unset this field from the data array:

if (empty($this->data['size'])) {
    unset($this->data['size']);
}
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
mowe_a 14 Feb, 2025

Hi Max,

thank you for your very quick reply. I might be wrong, but I think with unsetting the field I cannot update the database field to be null?

In case the 'size' in the db has the value 4 and I delete this value in the form field. When I unset the {data:size}, then the database won't be updated with null?

Max_admin 14 Feb, 2025

you are correct, this is a bug, but I have fixed it for the next update.

When do you need a working patch ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
mowe_a 15 Feb, 2025

Hi Max,

thank you very much for investigating in this. Would be great to have a patched version during the next week.

You need to login to be able to post a reply.