isset()没有给出预期的结果

时间:2015-07-30 07:46:20

标签: php mysql

我有一张表格

<form action="addnote.php" method="post">
    <input type="hidden" name="tid" <?php echo"value='$id'"; ?> >
    <div class="input_fields_wrap1">
        <label>Note</label>
        <input type="text" id="coa" name="notes[]" >
        <button class="add_field_button1">Add More Notes</button>
    </div>
    <label>Next call date</label>
    <input type="date" name="call" >
    <div class="form-group">
        <label class="col-md-3 control-label" for="example-text-input"></label>
        <div class="col-md-3">
            <input input type="submit" value="Submit " >
        </div>
    </div>
</form>

这是php代码

<?php
    include 'db.php';
    $id=$_POST['tid'];
    $notes=$_POST['notes'];
    if(isset($_POST['notes']))
    {
        for($i=0;$i<count($notes);$i++)
        {
            $sql="insert into notes (lead_id,note) value ('$id','$notes[$i]')";
            $var=mysqli_query($conn, $sql);
        }
    }
    if(isset($_POST['call']))
    {
        $call=mysqli_real_escape_string($conn,$_POST['call']);
        $sql="update lead set `call`='".$call."' where lead_id='$id'";
        $var=mysqli_query($conn, $sql);
    }
    header("Location: remove_lead.php");
?>

现在,即使我没有填写datenotes文本框,date仍会更新为0000-00-00,并且数据库中会存储空注。

但我想要的是:如果我不填写其中一个或两个,那么在php上什么都不会发生。我是否正确使用isset()

我改变了建议,但没有使用

if(!empty($_POST['notes'])&&isset($_POST['notes']))
{
    $notes=$_POST['notes'];
    for($i=0;$i<count($notes);$i++)
    {
        $sql="insert into notes (lead_id,note) value ('$id','$notes[$i]')";
        $var=mysqli_query($conn, $sql);
}
}
if(!empty($_POST['call'])&&isset($_POST['call']))
{
    $call=mysqli_real_escape_string($conn,$_POST['call']);
    $sql="update lead set `call`='".$call."' where lead_id='$id'";
    $var=mysqli_query($conn, $sql);
}

6 个答案:

答案 0 :(得分:3)

isset()不检查empty()它只会检查变量或数组或对象是否设置

您需要检查empty

if(isset($_POST['notes']) && !empty($_POST['notes']))

答案 1 :(得分:2)

编辑:

问题不在于您使用isset()

日期问题:

问题出在这行代码中。 $call=mysqli_real_escape_string($conn,$_POST['call']);

将其更改为$call=$_POST['call'];

请勿在日期值上使用mysqli_real_escape_string()

注意问题:

for($i=0;$i<count($notes);$i++)
        {
            $sql="insert into notes (lead_id,note) value ('$id','$notes[$i]')";
            $var=mysqli_query($conn, $sql);
        }

如果lead_id是主键或唯一,您将无法插入两次,或者多个lead具有相同的lead_id。只插入一个。插入和更新只会为您提供一个note

还:您的表单中有拼写错误。在<input input type="submit" value="Submit " >

还:以下代码有效。

<input type="hidden" name="tid" <?php echo"value='$id'"; ?> >

但请考虑将其更改为:

<input type="hidden" name="tid" value="<?php echo $id; ?>" >

答案 2 :(得分:0)

isset()用于检查是否已设置该变量,并且它与空值无关。如果它发现notes输入类型仍为空值,则表示条件为真。

您需要检查if(isset($_POST['notes']) && $_POST['notes']!='')

答案 3 :(得分:0)

你可以使用这样的东西。正确使用isset并正确使用空函数。

<?php
        include 'db.php';
        $id=isset($_POST['tid']) ? $_POST['tid'] : '';
        $notes=isset($_POST['notes']) ? $_POST['notes'] : '';
        $call = isset($_POST['call']) ? $_POST['call'] : '';
        if(!empty($id) && !empty($notes))
        {
            for($i=0;$i<count($notes);$i++)
            {
                $sql="insert into notes (lead_id,note) value ('$id','$notes[$i]')";
                $var=mysqli_query($conn, $sql);
            }
        }
        else
        {
            echo "Something was empty";
        }
        if(!empty($call))
        {
            $call=mysqli_real_escape_string($conn,$_POST['call']);
            $sql="update lead set `call`='".$call."' where lead_id='$id'";
            $var=mysqli_query($conn, $sql);
        }
        else
        {
            echo "Call was empty";
        }
        header("Location: remove_lead.php");
    ?>

编辑: - 您还有一份非工作表格

<form action="addnote.php" method="post">
    <input type="hidden" name="tid" value="<?php echo $id; ?>" >
    <div class="input_fields_wrap1">
        <label>Note</label>
        <input type="text" id="coa" name="notes[]" >
        <button class="add_field_button1">Add More Notes</button>
    </div>
    <label>Next call date</label>
    <input type="date" name="call" >
    <div class="form-group">
        <label class="col-md-3 control-label" for="example-text-input"></label>
        <div class="col-md-3">
            <input input type="submit" value="Submit " >
        </div>
    </div>
</form>

我不确定你是如何获得变量$ id的。

答案 4 :(得分:0)

isset()仅检查变量是否存在

并且您必须使用empty(),它会检查其中是否有任何值?

所以改变你的if语句就像这样

if(isset($_POST['notes']) && !empty($_POST['notes']))

答案 5 :(得分:0)

您无法通过isset();检查变量是否为空。您必须使用is_empty();方法才能找到它。

相关问题