PHP中的错误比较

时间:2013-12-03 20:13:27

标签: php post syntax

我已经创建了一个PHP函数来将一些数据存储在数据库中。但是,当我测试它时,比较出错了。它应该发现一个帖子包含一个带有false的布尔值。当它在屏幕上打印时,它看起来很好,但是,比较仍然是错误的。这是我认为很重要的代码。

PHP函数:

$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;
}
}

print_r函数显示良好的值。所有数据库查询都是正确的。在此之前已经制作了$ un。在此之前也建立了连接。

HTML表单:

<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>';
}

表单由PHP制作,以确保人们登录。 变量都已设置。有一点JavaScript,如果按下Delete this account按钮,它会将remove值更改为true,但这并不重要。 我曾尝试使用if($ _ POST [&#39; remove&#39;])而不是if($ _ POST [&#39; remove&#39;] == true),但它没有效果。我检查了我的语法,但似乎没有错。 这里有什么问题?为什么我总能成真?

1 个答案:

答案 0 :(得分:3)

它返回true,因为有一个与$ _POST ['remove']相关联的值 - 除非没有值,或者该值设置为false,它将始终返回true。

尝试===而不是==:)

编辑:示例:

<?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 (===)
   */