是否可以使用 PHP 将 HTML SELECT/OPTION 值设为 NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8796707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:31:21  来源:igfitidea点击:

Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?

phphtml-select

提问by Bernard

The following is a snippet from my HTML form which pulls the options from the table rows accordingly.

以下是我的 HTML 表单中的一个片段,它相应地从表格行中提取选项。

What I want to do is have the first option value to be NULL so when no choice is made, NULL is entered when the form is submitted to the database.

我想要做的是让第一个选项值为 NULL,所以当没有选择时,当表单提交到数据库时输入 NULL。

    <td>Type</td>
    <td>:</td>
    <td><select name="type_id" id="type_id" class="form">
    <option value=""></option>
        <?php 
            $sql = mysql_query("SELECT type_id, type FROM $tbl_add_type") or die(mysql_error());
            while ($row = mysql_fetch_array($sql))
                {
                    echo "<option value=".$row['type_id'].">" . $row['type'] . "</option>";
                }
        ?>
    </select>*</td>

Is this possible? Or could someone suggest an easier/better way of doing this?

这可能吗?或者有人可以建议一种更简单/更好的方法吗?

Thanks

谢谢

Update: Thanks for the answer, I'm using the method detailed below to convert it to NULL.

更新:感谢您的回答,我正在使用下面详述的方法将其转换为 NULL。

if ($_POST['location_id'] === '')
                    {
                        $_POST['location_id'] = 'NULL';
                    }

However when trying to use this NULL value with the following query it does not work.

但是,当尝试将此 NULL 值用于以下查询时,它不起作用。

UPDATE addresses SET location_id='NULL' WHERE ipid = '4791'

I know it needs to be location_id=NULLinstead but don't know how to do this...

我知道它需要location_id=NULL改为但不知道如何做到这一点......

Update 2: this is how my queries work:

更新 2:这就是我的查询的工作方式:

if ($_POST['location_id'] === '')
{
$_POST['location_id'] = 'NULL'; // or 'NULL' for SQL
}

$notes=isset($_POST['notes']) ? mysql_real_escape_string($_POST['notes']) : '';
//$location_id=isset($_POST['location_id']) ? mysql_real_escape_string($_POST['location_id']) : '';
$ipid=isset($_POST['ipid']) ? mysql_real_escape_string($_POST['ipid']) : '';


$sql="UPDATE addresses 
    SET notes='$notes', location_id='$location_id'
    WHERE ipid = '$ipid'";


mysql_query($sql) or die(mysql_error());

采纳答案by deceze

No, POST/GET values are never null. The best they can be is an empty string, which you can convert to null/'NULL'.

不,POST/GET 值永远不会null。它们最好是一个空字符串,您可以将其转换为null/ 'NULL'

if ($_POST['value'] === '') {
    $_POST['value'] = null; // or 'NULL' for SQL
}

回答by Jason

All you need is a check on the post side of things.

您所需要的只是对事物的邮政方面的检查。

if(empty($_REQUEST['type_id']) && $_REQUEST['type_id'] != 0)
    $_REQUEST['type_id'] = null;

回答by Michael Walter

Yes, it is possible. You have to do something like this:

对的,这是可能的。你必须做这样的事情:

if(isset($_POST['submit']))
{
  $type_id = ($_POST['type_id'] == '' ? "null" : "'".$_POST['type_id']."'");
  $sql = "INSERT INTO `table` (`type_id`) VALUES (".$type_id.")";
}

It checks if the $_POST['type_id']variable has an empty value. If yes, it assign NULLas a string to it. If not, it assign the value with ' to it for the SQLnotation

它检查$_POST['type_id']变量是否具有空值。如果是,则将其NULL作为字符串分配给它。如果不是,它为SQL符号分配带有 ' 的值

回答by Jose Antonio Garcia Escandon

In php 7 you can do:

在 php 7 中,您可以执行以下操作:

$_POST['value'] ?? null;

If value is equal to '' as said in other answers it will also send you null.

如果 value 等于 '' ,如其他答案中所述,它也会向您发送 null。

回答by Your Common Sense

that's why Idon't like NULL values in the database at all.
I hope you are having it for a reason.

这就是为什么我根本不喜欢数据库中的 NULL 值。
我希望你有它的原因。

if ($_POST['location_id'] === '') {
  $location_id = 'NULL';
} else {
  $location_id = "'".$_POST['location_id']."'";
}
$notes = mysql_real_escape_string($_POST['notes']);
$ipid  = mysql_real_escape_string($_POST['ipid']);

$sql="UPDATE addresses 
    SET notes='$notes', location_id=$location_id
    WHERE ipid = '$ipid'";

echo $sql; //to see different queries this code produces
// and difference between NULL and 'NULL' in the query