【问题标题】:If $_POST null form input codeigniter如果 $_POST null 表单输入 codeigniter
【发布时间】:2019-12-02 04:16:48
【问题描述】:

如果 POST NULL 和其他 POST 值怎么办?

$data = array(
        'harga_jual' => $this->input->post('harga_jual') == '' ? NULL : $this->input->post('harga_jual')
        );

谢谢

【问题讨论】:

    标签: php mysql codeigniter if-statement


    【解决方案1】:

    或者如果您的 php 版本 >= 7.0,您可以使用来自 this article 的 Null Coalescing Operator。

    <?php
    // Fetches the value of $_GET['user'] and returns 'nobody'
    // if it does not exist.
    $username = $_GET['user'] ?? 'nobody';
    // This is equivalent to:
    $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
    
    // Coalescing can be chained: this will return the first
    // defined value out of $_GET['user'], $_POST['user'], and
    // 'nobody'.
    $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
    ?>
    

    【讨论】:

      【解决方案2】:

      您需要将null 放在引号中。您可以尝试以下方法-

      $harga_jual = $this->input->post('harga_jual') ? $this->input->post('harga_jual') : 'null'; //you can use $_POST['harga_jual'] also
      
      $data = array('harga_jual' => $harga_jual);
      

      【讨论】:

        【解决方案3】:

        您可以使用 is_null() 来检查 POST 值是否为空。

        $data = array(
            'harga_jual' => is_null($this->input->post('harga_jual')) ? NULL : $this->input->post('harga_jual')
            );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-01
          • 1970-01-01
          • 2012-11-14
          • 1970-01-01
          • 2011-06-01
          • 1970-01-01
          • 2016-03-25
          相关资源
          最近更新 更多