Skip to content
Advertisement

CodeIgniter – how to post value from form as NULL instead of 0

In my database it is a table: tab_companies and a column company_phone (big integer) with defalut value set to NULL.

When somebody fill the form and will leave the phone number field empty Code Igniter should add a NULL value to the database, but instead I am having always value ‘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);   
}

What I am doing wrong?

Advertisement

Answer

you could do, set it to NULL in case its empty, like:

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

...
'company_phone'  => (!empty($company_phone)) ? $company_phone : NULL,
...
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement