D-Inventor
D-Inventor

Reputation: 480

Wrong comparison in PHP

I have made a PHP function to store some data in a database. However, when I tested it, a comparison went wrong. It should find that a post contains a boolean with false. when printing it on screen, it looks just fine, however, the comparison is still wrong. here is the code I beleve is important.

PHP function:

$FaultMss = "";
$chpw = "";
if($login){
if(isset($_POST['Save_Changes'])){

    $nun = $db->real_escape_string($_POST['nun']);
    $nem = $db->real_escape_string($_POST['nem']);
    $ncn = $db->real_escape_string($_POST['ncn']);
    $nct = $db->real_escape_string($_POST['nct']);

    if(isset($_POST['chpw'])){
        $chpw = "checked";
        $opw = $_POST['opw'];
        $npw = $_POST['npw'];
        $cnpw = $_POST['cnpw'];
    }

    print_r($_POST['remove']);    //This outputs the correct value
    if($_POST['remove'] == true){   //Here it goes wrong. I should get false when pressing the html link "save changes". But I always get true.

        print_r($_POST['remove']);   //This outputs the correct value
        $FaultMss = "A removement";

    }else{

        $FaultMss = "Not a removement";

    }
}else{

    $uns = $db->real_escape_string($un);
    $Result = mysqli_query($db, "SELECT * FROM `accounts` WHERE `Username`='" . $uns . "'");
    $row = $Result->fetch_object();
    $nun = $un;
    $nem = $row->Email;
    $ncn = $row->Country;
    $nct = $row->City;
}
}

The print_r function shows the good value. All database querys are correct. $un has been made before this. And the connection is made before this as well.

HTML form:

<div id="Personal_info_wrapper" style="overflow:auto; position:absolute; top:40; left:40; width:460; height:430;">
<?php 

if($login){
    echo '<form id="personalInfo" action="" method="POST">
    <label for="un">Username:</label><br/>
        <input type="text" id="un" name="nun" value="' . $nun . '"/><br/>
    <br/>
        <input type="checkbox" id="chpw" name="chpw" value=true ' .$chpw. '/><label for="chpw">I want to change my password</label><br/>
    &nbsp;<label for="opw">Old password</label><br/>
        &nbsp;<input type="password" id="opw" name="opw"/><br/>
    &nbsp;<label for="npw">New password</label><br/>
        &nbsp;<input type="password" id="npw" name="npw"/><br/>
    &nbsp;<label for="cnpw">Verify new password</label><br/>
        &nbsp;<input type="password" id="cnpw" name="cnpw"/><br/>
    <label for="em">E-mail:</label><br/>
        <input type="email" id="em" name="nem" value="' . $nem . '" style="width:200px;"/><br/>
    <label for="cn">Country:</label><br/>
        <input type="text" id="cn" name="ncn" value="' . $ncn . '"/><br/>
    <label for="ct">City:</label><br/>
        <input type="text" id="ct" name="nct" value="' . $nct . '"/><br/>
        <input type="hidden" name="Save_Changes" value=true/>
        <input type="hidden" name="Tab" value=4/>
        <input type="hidden" id="remove" name="remove" value=false />
    <br/>
    <a href=\'Javascript:document.forms["personalInfo"].submit();\'>Save changes</a>  &nbsp;  <a href="#" onclick="ConfirmRemoval()">Delete this account</a><br/>
    ' . $FaultMss . '</center>
    </form>';
}

The form is made by PHP to make sure people are logged in. The variables are all set. There is a little bit of JavaScript, which changes the remove value to true if the Delete this account button is pressed, but that is not important. I have tried to use if($_POST['remove']) instead of if($_POST['remove'] == true), but it had no effect. I have checked my syntax, but nothing seems to be wrong. What is the problem here? Why do I always get true?

Upvotes: 0

Views: 90

Answers (1)

skrilled
skrilled

Reputation: 5371

It returns true because there is a value associated with $_POST['remove'] - unless there is no value, or the value is set to false it will always return true.

Try === instead of == :)

Edit: example:

<?php
   $lol = 'man this is a string';
   echo ($lol) ? 'true (==)' : 'false (==)'; echo "\n";
   echo ($lol===true) ? 'true (===)' : 'false (===)';

   /*
     Output:
     true (==)
     false (===)
   */

   $lol = true;
   echo ($lol) ? 'true (==)' : 'false (==)'; echo "\n";
   echo ($lol===true) ? 'true (===)' : 'false (===)';

   /*
     Output:
     true (==)
     true (===)
   */

Upvotes: 3

Related Questions