Forums

Zero in DB on save empty nummeric field

Melgui 26 Mar, 2012
Dear all,

please help me with following issue. I use default event (DB Save) for saving data to database.
But I have a trouble with a nummeric fields. On saving empty value I allways have zero in my integer column. I have a idea how it could be work if i would save all data in custom way, but its not my goal.

Please help me!

Thanks
Alex
GreyHead 26 Mar, 2012
Hi Alex,

Do you have a default value or NOT NULL set for the column?

Please drag a Debugger action into the On Submit event, then submit the form and post the debug - including the 'dummy emails' results here. Note: if you are using the Easy Wizard you may need to switch to the Advanced Wizard to do this; if you want to continue to use the Easy Wizard please make a copy of your form first and add the Debugger action to the copy.

Bob
Melgui 26 Mar, 2012
Default value is nothing.
In debugger looks good, but in db I still have zero😟


Settings


DB Settings
GreyHead 26 Mar, 2012
Hi Melgui ,

And is there a default value in the DB Settings?

Bob
Melgui 26 Mar, 2012
Yes, it is. NULL is a dauflt value.
Melgui 27 Mar, 2012
I can't belive that I am alone with this question! How save other users empty values in integer field with DB Save event.
:(
GreyHead 27 Mar, 2012
Hi Melgui,

I've never seen it reported here before - at least I don't remember it.

Bob
Melgui 05 Apr, 2012
This issues is very populer in internet. For cusom save there are a trick like:

++++
...Yes, form fields are always Strings. So when you submit them without inputting anything, you are actually submitting '' (an empty string) which gets translated into 0. You should include a simple condition to make sure the input is not empty, and take appropriate action if it does.

if (empty($_POST['input'])) { $variable_to_pass = null; }
...
+++

But I use DB Save event. What else can I do?
Thank you Bob
GreyHead 05 Apr, 2012
Hi Melgui,

You can add a Custom Serverside validation action (or a Custom code action) and use this code:
<?php
$integer_array = array(
  'input_one',
  . . . 
);
foreach ( $integer_array as $input ) {
  if  ( !isset($form->data[$input]) || empty($form->data[$input]) ) {
    $form->data[$input] = null;
  }
}
?>

Bob
Melgui 05 Apr, 2012
I have tried to implemt this code. It works, but nor 100%.
If I load my data at first time, so all fields are empty. On save empty fields all field are empty as befor, thats good.
But wenn I try to change the value (that I save befor) from "6 to empty" and then save that task, so I have in DB "6".
Whats going wrong on this place.

I have set custom code on submit befor DB Save.
code looks as following:
<?php
$integer_array = array('intValue1','intValue2');
foreach ( $integer_array as $input ) {
  if  ( !isset($form->data[$input]) || empty($form->data[$input]) ) {
    $form->data[$input] = null;
  }
}
?>
GreyHead 05 Apr, 2012
Hi melgui,

I'm sorry, I don't understand what the problem is :-(

Bob
Melgui 05 Apr, 2012
I am sorry for my english πŸ˜€ πŸ˜€

If the value is already nothing, so it works.
But on changing value to nothing, doesn't happens.

Nothing to Nothing = Nothing
Value to Nothing = old value
GreyHead 05 Apr, 2012
Hi Melgui ,

Maybe empty() isn't the correct test here? Is $form->data[$input] == '' is better?

Bob
Max_admin 06 Apr, 2012
Hi Melgui,

"Value to Nothing = old value" because you set the field value to "Null", this will assume that the field doesn't exist in the post array and its value will be ignored, it will not be updated in the database.

Set it to "" or 0 instead.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.