当前位置: 动力学知识库 > 问答 > 编程问答 >

php - isset() not giving expected results

问题描述:

I have a form

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

and this is the php code

<?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");

?>

Now, even if I don't fill the date or notes text box, date still gets updated to 0000-00-00 and an empty note is stored in database.

But what I want is: If I don't fill one or both of them, then nothing should happen on php. Did I use isset() correctly?

I changed as suggested but no use

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

}

网友答案:

isset() does not check for empty() it does only check whether the variable or array or object is set.

you need to check for empty.

if(isset($_POST['notes']) && !empty($_POST['notes']))
网友答案:

EDIT:

The problem is not with your use of isset().

DATE PROBLEM:

The problem is in this line of code. $call=mysqli_real_escape_string($conn,$_POST['call']);

Change it to $call=$_POST['call'];

DO NOT USE mysqli_real_escape_string() on date values.

NOTES PROBLEM:

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

If lead_id is a primary key or unique you wont be able to insert it twice, or have multiple lead with the same lead_id. Only one will be inserted. Insert and update will only give you one note.

also: There is a typo in your form. in the <input input type="submit" value="Submit " >.

also: The code below works.

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

But consider changing it to:

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

网友答案:

isset() is used to check whether that variable has been set or not and it doesn't concern with empty value. If it found notes input type with blank value still it will make condition true.

You need to check if(isset($_POST['notes']) && $_POST['notes']!='')

网友答案:

isset() only checks if a variable exists or not

and you will have to use empty(), it checks that is there any value in it or not?

So change your if statements like so

if(isset($_POST['notes']) && !empty($_POST['notes']))
网友答案:

You can use something like this. Proper use of isset and proper use of empty function.

<?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");
    ?>

EDIT :- You have a non-working form as well

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

I'm not sure how you're getting the variable $id though.

网友答案:

You cannot check variable is empty by isset();. You have to use is_empty(); method to find it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: