繁体   English   中英

Codeigniter插入空值,MySQL中表字段的默认值被忽略

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

我正在对表进行插入,一些数据是空的,所以我希望一些字段能够获得我分配的默认值。

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

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

问题在于用户字段,该字段的默认值为“匿名”,但是当 post('name') 中没有数据时,会插入一个空值,而不是字段中的默认值。

用户字段为 varchar,null。

这会做吗?

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

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

这可能与post方法有关。 name为空或未定义时,它将返回一个空字符串。 然后在列中输入。

尝试这个

$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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM