CodeIgniter Forums
Active records problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active records problem (/showthread.php?tid=29570)



Active records problem - El Forum - 04-14-2010

[eluser]umefarooq[/eluser]
Hi im facing one problem with CI database active record is while inserting and updating any records im sending some fields just empty , where these field default value is NULL in table when query executed successfully but it add 0 to most of fields who's value im sending empty i want Null value to be insert.


Active records problem - El Forum - 04-14-2010

[eluser]umefarooq[/eluser]
its really strange but figure out just looping the post data and checking if is empty assign NULL just a simple example

Code:
$data['content'] = ($this->input->get_post('content') == '')?NULL:$this->input->get_post('content');

but i need more good explanation


Active records problem - El Forum - 04-14-2010

[eluser]mddd[/eluser]
Inserting an empty string into a database field is different from inserting NOTHING into that field.
If you insert nothing, or you don't even specify the field at all in your INSERT query, then you will get the default value.
If you DO insert something (namely an empty string) then THAT will be inserted. (And if the field is not a text/char field, the empty string will be translated to something that does match the type, like 0).

Consider this: if you inserted an empty string into a text field and that text field had a default value, what should mysql do? Well, save the empty string of course! That is what you put in there. It won't put in a default value if you specify a value. Even if your value is an empty string, or 0.