【发布时间】: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
如果 POST NULL 和其他 POST 值怎么办?
$data = array(
'harga_jual' => $this->input->post('harga_jual') == '' ? NULL : $this->input->post('harga_jual')
);
谢谢
【问题讨论】:
标签: php mysql codeigniter if-statement
或者如果您的 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';
?>
【讨论】:
您需要将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);
【讨论】:
您可以使用 is_null() 来检查 POST 值是否为空。
$data = array(
'harga_jual' => is_null($this->input->post('harga_jual')) ? NULL : $this->input->post('harga_jual')
);
【讨论】: