Welcome Guest, Not a member yet? Register   Sign In
$this->input->post() null /mysql date type issue
#1

[eluser]Unknown[/eluser]
Hi all,

I've got the following code:

Code:
$data = array(
  'some_date' => $this->input->post('some_date')
);

$this->db->insert( 'my_table', $data );

This executes fine however because the 'some_date' column is type 'date' and does not allow NULL values, if a value is not entered in the form this is getting filled as '0000-00-00' because rather than getting sent NULL the database is getting an empty string.

I could get around this by doing some sort of conditional on the data before it goes into the $data array to not send data for this cell:

Code:
if ( $this->input->post('some_date') !== '' )
  $data['some_date'] = $this->input->post('some_date');

but it doesn't seem very DRY as I ideally I'd like this behaviour on each array element.

Does anyone know how I could possibly achieve this by default or via some sort of filter please?

Thanks in advance.
#2

[eluser]Aken[/eluser]
You could fill your $data array, and then go over it with a loop or an array_map() function to check for empties.

Ideally, you will ALWAYS do some sort of filtering / sanitizing / checking on any element that will go into your DB. I think a DRY rule in this syntax is thinking a little too extreme. Sometimes those items will need their own specific checks, so you may find yourself doing various IF checks regardless.
#3

[eluser]CroNiX[/eluser]
Sounds like it could be fixed with a simple validation callback, applied only to those fields that need it.




Theme © iAndrew 2016 - Forum software by © MyBB