开发者

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(开发者_C百科'name') an empty value is inserted instead of the default value in the field.

User field is varchar, null.


Would this do it?

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

if ($user = $this->input->post('name')) {
   $data['user'] = $user;
}


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.


Try this

$default_user = "Anon";
$username = $this->input->post('name');
if($username == empty) ? $username = $default_user : $this->input->post('name'));
$data = array(
            'id'            => $id,
            'user'       => $this->input->post('name'),
            );

$this->the_model->the_model_function($data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜