使用PHP是否可以将HTML SELECT/OPTION值设置为NULL? [英] Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?

查看:224
本文介绍了使用PHP是否可以将HTML SELECT/OPTION值设置为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的HTML表单中的代码段,该代码段相应地从表行中提取了选项.

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

我要做的是将第一个选项的值设置为NULL,因此,如果没有选择,则在将表单提交到数据库时输入NULL.

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.

    <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?

谢谢

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

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

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

但是,当尝试在下面的查询中使用此NULL值时,它将不起作用.

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

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

我知道它必须是location_id=NULL,但不知道该怎么做...

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

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

Update 2: this is how my queries work:

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());

推荐答案

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

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

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

这篇关于使用PHP是否可以将HTML SELECT/OPTION值设置为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆