【问题标题】:CodeIgniter - how to post value from form as NULL instead of 0CodeIgniter - 如何将表单中的值发布为 NULL 而不是 0
【发布时间】:2014-08-16 23:55:46
【问题描述】:

在我的数据库中,它是一个表:tab_companies 和一列 company_phone(大整数),默认值设置为 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);   
}

我做错了什么?

【问题讨论】:

  • 数据库行是否设置为NOT NULL?
  • 数据库列 'company_phone' 设置为默认值 NULL。当有人添加新公司并将电话号码留空时,我希望数据库中有一个空值,而不是零。

标签: php mysql database forms codeigniter


【解决方案1】:

你可以这样做,将其设置为NULL,以防它为空,例如:

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

...
'company_phone'  => (!empty($company_phone)) ? $company_phone : NULL,
...

【讨论】:

  • company_short-name 不是问题,因为它是一个文本值,但是我在将 NULL 设置为 company_phone时遇到问题>
  • @Tikky 这只是一个例子,你可以对 company_phone 做同样的事情
  • @Sudhir: 谢谢 - 即使数据库设置为 Null 作为默认值,我也不知道我需要设置为 null。你的解决方案对我来说很好:)
【解决方案2】:

我刚刚找到了一个自动解决方案。您只需通过覆盖post() 方法来扩展CI_Input 类。

/application/core/MY_Input.php 中创建一个MY_Input

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

【讨论】:

【解决方案3】:
//empty to null로
function empty2null($v){
    return empty($v) ? null : $v;
}

【讨论】:

  • 当我发布一个数组并且其中一个键为空时,它对我不起作用,它仍然插入 0,让我发疯
【解决方案4】:

根据上面 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')
];

【讨论】:

  • 注意你提交的空整数。 empty('')转换后返回0
【解决方案5】:

此线程提供信息:https://forum.codeigniter.com/thread-72905.html

我所做的是尝试直接在post() 函数中修复数据。所以我复制了原始函数,在 MY_Input.php 中对其进行了扩展,并使用此函数将递归空字符串 '' 转换为 NULL(以防您通过表单提交数组)。它工作得很好,imo应该在codeigniter中。

        private function convertEmptyStrToNull($variable_to_fix)
    {
        if (is_array($variable_to_fix))
        {
            foreach ($variable_to_fix as $key => $item)
            {
                $fixed_variable[$key] = $this->convertEmptyStrToNull($item);
            }
        }
        else
        {
            $fixed_variable = ($variable_to_fix === '') ? NULL : $variable_to_fix; // NOTE: change to a random number to debug, else can't see in log empty ''
        }

        return $fixed_variable;
    }

然后在post() 中,您通过它传递您的值,而不是返回return $this->_fetch_from_array($_POST, $index, $xss_clean);

        public function post($index = NULL, $xss_clean = FALSE)
    {
        // Check if a field has been provided. It's for post() without any argument
        if ($index === NULL AND ! empty($_POST))
        {
            $post = array();

            // Loop through the full _POST array and return it
            foreach (array_keys($_POST) as $key)
            {
                $key_value = $this->_fetch_from_array($_POST, $key, $xss_clean);

                $post[$key] = $this->convertEmptyStrToNull($key_value);
            }

            return $post;
        }

        // IF A FIELD HAS BEEN PROVIDED (ex: post('name))
        $post_value = $this->_fetch_from_array($_POST, $index, $xss_clean);

        return $this->convertEmptyStrToNull($post_value);
    }

注意:您当然可以手动修复每个输入,但由于这是我第二次在此浪费时间,所以我想找到一个全局解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2017-08-31
    相关资源
    最近更新 更多