Welcome Guest, Not a member yet? Register   Sign In
Error when INSERT empty date on MySQL field with Active Record
#1

[eluser]Massimiliano Marini[/eluser]
Hi all,

In my form I've a textbox where it is possible to insert a date but it's optional.

When I do:

Code:
$event = array(
  'name' => $this->input->post('name'),
  'date_event' => $this->input->post('date_event')
);

$this->db->insert('events', $event);

I got this error:

Code:
A Database Error Occurred

Error Number: 1292

Incorrect date value: '' for column 'date_event' at row 1

INSERT INTO `events` (`name`, `date_event`) VALUES ('Foo', '');

My MySQL table is:

Code:
create table event(
  name varchar(128),
  date_event date default null, // I also have tried: null default '0000-00-00' nut nothing
);

but nothing, I can't insert my data into the table.

Why?
#2

[eluser]Madmartigan1[/eluser]
Cast the value to integer.

Code:
$data['event_date'] = (integer) $this->input->post('event_date');

It will default to 0 if the value is empty.

EDIT: Sorry I use unix timestamps, I'm actually not familiar with the MySQL "date" type. I didn't read your post thoroughly.




Theme © iAndrew 2016 - Forum software by © MyBB