Php CodeIgniter-如何将表单中的值作为NULL而不是0发布
在我的数据库中,它是一个表:tab_companys和列company_phone(大整数),defalut值设置为NULL 当有人填写表单并将电话号码字段留空时,代码点火器应向数据库中添加一个空值,但我始终使用值“0”Php CodeIgniter-如何将表单中的值作为NULL而不是0发布,php,mysql,database,forms,codeigniter,Php,Mysql,Database,Forms,Codeigniter,在我的数据库中,它是一个表:tab_companys和列company_phone(大整数),defalut值设置为NULL 当有人填写表单并将电话号码字段留空时,代码点火器应向数据库中添加一个空值,但我始终使用值“0” public function add_company() { $data = array( 'company_short-name' => $this->input->post('company_short-name')
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
,以防其为空,如:
$company_phone = $this->input->post('company_phone');
...
'company_phone' => (!empty($company_phone)) ? $company_phone : NULL,
...
根据上面Sudhir的回答,我在助手文件中创建了一个小助手函数来简化:
function nullAllowedInput($name, $post = TRUE)
{
if($post)
return (empty($_POST["$name"])) ? NULL : get_instance()->input->post("$name", TRUE);
return (empty($_GET["$name"])) ? NULL : get_instance()->input->get("$name", TRUE);
}
必要时从控制器调用:
// POST
// load the helper file here or via autoload.php
$data = [
'name' => nullAllowedInput('name'),
'phone' => nullAllowedInput('phone')
];
我刚刚找到了一个自动解决方案。您只需通过重写
post()
方法来扩展CI\u Input
类
在/application/core/MY_Input.php
class MY_Input extends CI_Input {
public function post($index = NULL, $xss_clean = NULL)
{
$value= $this->_fetch_from_array($_POST, $index, $xss_clean);
return $value === '' ? null : $value;
}
}
从现在起,每个空的
POST
值都将转换为NULL
此线程提供以下信息:
//empty to null로
function empty2null($v){
return empty($v) ? null : $v;
}
我所做的是尝试直接在post()函数中修复数据。因此,我复制了原始函数,在MY_Input.php中对其进行了扩展,并使用此函数将递归空字符串“”转换为NULL(以防通过表单提交数组)。它工作得很好,imo应该在codeigniter中
私有函数convertEmptyStrToNull($variable\u to\u fix)
{
if(is_数组($variable_to_fix))
{
foreach($variable\u to\u fix as$key=>$item)
{
$fixed_variable[$key]=$this->convertEmptyStrToNull($item);
}
}
其他的
{
$fixed\u variable=($variable\u to\u fix==='')NULL:$variable\u to\u fix;//注意:更改为随机数以进行调试,否则无法在日志中看到空''
}
返回$fixed_变量;
}
然后在post()
中,通过该数组传递值,而不是返回return$this->\u fetch\u from_数组($\u post,$index,$xss\u clean)代码>
公共函数post($index=NULL,$xss_clean=FALSE)
{
//检查是否提供了字段。该字段用于post(),没有任何参数
如果($index==NULL和!空($\u POST))
{
$post=array();
//循环遍历完整的_POST数组并返回它
foreach(数组\u键($\u POST)作为$key)
{
$key\u value=$this->\u fetch\u from\u数组($\u POST,$key,$xss\u clean);
$post[$key]=$this->convertEmptyStrToNull($key\u值);
}
退回$post;
}
//如果提供了字段(例如:post(‘名称))
$post_value=$this->_fetch_from_数组($_post,$index,$xss_clean);
返回$this->convertEmptyStrToNull($post_值);
}
注意:您当然可以手动修复每个输入,但由于这是我第二次在这方面浪费时间,我想找到一个全局解决方案。数据库行是否设置为非空?数据库列“company\u phone”设置为默认值空。当有人添加一个新公司并将电话号码保留为空时,我希望数据库中有一个空值,而不是零。公司的简称不是问题,因为它是一个文本值,但是我在将空值设置为公司时遇到问题_phone@Tikky这只是一个例子,你也可以为公司做同样的事情_phone@Sudhir:谢谢-不知道我需要设置为null,即使数据库作为默认值设置为null。你的解决方案对我来说很好:)注意你提交的空整数empty(“”)
在转换后返回0当我发布一个数组且其中一个键为null时,它对我不起作用,它仍然插入0,这对我不起作用当我发布一个数组且其中一个键为null时,它仍然插入0,这对我有帮助,ty