CodeIgniter-如何将表单中的值发布为NULL而不是0 [英] CodeIgniter - how to post value from form as NULL instead of 0

查看:37
本文介绍了CodeIgniter-如何将表单中的值发布为NULL而不是0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当某人填写表格并将电话号码字段保留为空时,代码点火器应向数据库添加NULL值,但我的值始终为'0'.

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

我做错了什么?

推荐答案

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

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,
...

这篇关于CodeIgniter-如何将表单中的值发布为NULL而不是0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆