Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter inserts empty value, default value of table field in MySQL is ignored

I'm doing an insert to a table, some data is empty so I expect some fields to get the default value I assigned.

$data = array(
            'id'            => $id,
            'user'       => $this->input->post('name'),
            );

$this->the_model->the_model_funciton($data);

The problem is with the user field, the default value for the field is 'Anon' but when there's no data in the post('name') an empty value is inserted instead of the default value in the field.

User field is varchar, null.

like image 260
Danny Avatar asked Dec 20 '25 06:12

Danny


2 Answers

Would this do it?

$data = array(
           'id'   => $id
       );

if ($user = $this->input->post('name')) {
   $data['user'] = $user;
}
like image 56
alex Avatar answered Dec 22 '25 20:12

alex


This is probably related to the post method. When name is empty or undefined it will return an empty string. Which then gets input in the column.

like image 27
rzetterberg Avatar answered Dec 22 '25 19:12

rzetterberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!