CodeIgniter - 如何将表单中的值作为NULL而不是0发布

Tik*_*kky 6 php mysql database forms codeigniter

在我的数据库中,它是一个表:tab_companies和一个company_phone(大整数)列,其中defalut值设置为NULL.

当有人填写表单并将电话号码字段留空时Code Igniter应该向数据库添加NULL值,但我总是将值设置为'0'.

public function add_company()
{
    $data = array(
        'company_short-name'        => $this->input->post('company_short-name'),
        'company_full-name'         => $this->input->post('company_full-name'),
        'company_phone'             => $this->input->post('company_phone'),
        'company_address'           => $this->input->post('company_address'),
    );

    return $this->db->insert('tab_companies', $data);   
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Sud*_*oti 6

您可以将其设置为NULL空,例如:

$company_phone = $this->input->post('company_phone');

...
'company_phone'  => (!empty($company_phone)) ? $company_phone : NULL,
...
Run Code Online (Sandbox Code Playgroud)