PHP Empty Checks总是返回Negative

时间:2016-03-28 15:43:33

标签: php html

Hello Stackoverflow,

我又来了,这一次是关于我创建的PHP函数。我试图让用户设置一个引脚,但是Form总是返回" A Field is Empty"错误。这很奇怪。一切正常,甚至其他错误检查,但空只返回负数。

这是我的PHP

if(isset($_POST['pin-submit'])){

    // Filter the Old PIN and IMPLODE it.

    $pinold_1 = htmlentities($_POST['pinold-1']);
    $pinold_2 = htmlentities($_POST['pinold-2']);
    $pinold_3 = htmlentities($_POST['pinold-3']);
    $pinold_4 = htmlentities($_POST['pinold-4']);

    $array_old = array($pinold_1, $pinold_2, $pinold_3, $pinold_4);
    $oldpin = implode("|", $array_old);

    // Filter the New PIN and IMPLODE it.

    $pinnew_1 = htmlentities($_POST['pinnew-1']);
    $pinnew_2 = htmlentities($_POST['pinnew-2']);
    $pinnew_3 = htmlentities($_POST['pinnew-3']);
    $pinnew_4 = htmlentities($_POST['pinnew-4']);

    $array_new = array($pinnew_1, $pinnew_2, $pinnew_3, $pinnew_4);
    $newpin = implode("|", $array_new);

    // Filter the New Repeated PIN and IMPLODE it

    $pinnewrep_1 = htmlentities($_POST['pinnewrep-1']);
    $pinnewrep_2 = htmlentities($_POST['pinnewrep-2']);
    $pinnewrep_3 = htmlentities($_POST['pinnewrep-3']);
    $pinnewrep_4 = htmlentities($_POST['pinnewrep-4']);

    $array_newrep = array($pinnewrep_1, $pinnewrep_2, $pinnewrep_3, $pinnewrep_4);
    $newreppin = implode("|", $array_newrep);

    // Explode the User's actual PIN into pieces.

    $array_actual = explode("|", $user['slp']);

    $pinactual_1 = $array_actual[0];
    $pinactual_2 = $array_actual[1];
    $pinactual_3 = $array_actual[2];
    $pinactual_4 = $array_actual[3];

    // Check if Actual PIN is the Actual PIN.

    if($user['slp'] !== $oldpin){

    echo "<div class='alert alert-dismissible alert-danger'>";  
    echo "<strong>Hey {username}!</strong> Je actuele PIN klopt niet.  Probeer het eventjes opnieuw.";
    echo "</div>";

    // Check if the New PIN is the Same as the Repeated New PIN.

    }else if($newpin !== $newreppin){

    echo "<div class='alert alert-dismissible alert-danger'>";  
    echo "<strong>Hey {username}!</strong> Je Nieuwe PIN komt niet overeen met de Herhaling van je nieuwe PIN.";

    echo "</div>";

    // Check if all Fields are filled in.

    }else if (empty($pinold_1) || empty($pinold_2) || empty($pinold_3) || empty($pinold_4) ||
    empty($pinnewrep_1) || empty($pinnewrep_2) || empty($pinnewrep_3) || empty($pinnewrep_4) ||
    empty($pinnew_1) || empty($pinnew_2) || empty($pinnew_3) || empty($pinnew_4)){

    echo "<div class='alert alert-dismissible alert-danger'>";  
    echo "<strong>Hey {username}!</strong> Je moet elk veld ingevuld hebben. Geen enkele mag leeg zijn!";
    echo "</div>";

    // Else Save the new PIN and show a Success message.

    }else{

    echo "<div class='alert alert-dismissible alert-success'>"; 
    echo "<strong>Hey {username}!</strong> Je PIN is gewijzigd. Je kan hem direct gebruiken.";
    echo "</div>";  

    }
}

这是我的HTML(包含表单)

<form id="pinForm" name="pin" method="post" class="form-horizontal">

<span class="help-block"><strong>Je Second Layer PIN Wijzigen</strong><br>Pas hier jouw SLP aan. Vul eerst je actuele PIN in, en daarna de gewenste nieuwe.</span>

<label class="control-label" for="inputEmail">Actuele PIN</label>
<div class="form-group">
<div class="col-lg-10">
<input type="text" name="pinold-1" style="width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinold-2" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinold-3" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinold-4" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<span class="help-block" style="float:right;"><small>Je Second Layer PIN die je <strong>nu gebruikt</strong> bij je Magie Account.</small></span>
</div></div>

<label class="control-label" for="inputEmail">Nieuwe PIN</label>
<div class="form-group">
<div class="col-lg-10">
<input type="text" name="pinnew-1" style="width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinnew-2" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinnew-3" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinnew-4" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<span class="help-block" style="float:right;"><small>Je gewenste PIN. Het beste is vier <strong>niet identieke</strong> Getallen.</small></span>
</div></div>

<label class="control-label" for="inputEmail">Herhaal Nieuwe PIN</label>
<div class="form-group">
<div class="col-lg-10">
<input type="text" name="pinnewrep-1" style="width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinnewrep-2" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinnewrep-3" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<input type="text" name="pinnewrep-4" style="margin-left:8px; width:30px;" maxlength="1" size="1" id="pinform">
<span class="help-block" style="float:right;"><small>Herhaal je nieuwe gewenste PIN. Dit is om fouten te <strong>voorkomen<strong>.</small></span>
</div></div>

<hr>
<div class="form-group">
<div class="col-lg-10">
<button name="pin-submit" type="submit" class="btn btn-primary">Opslaan</button>
</div>
</div>

</form>

提前致谢, 帕斯卡

2 个答案:

答案 0 :(得分:0)

如果var存在并且具有非空的非零值,则返回FALSE。否则返回TRUE。

注意非零部分。如果用户使用&#39; 0&#39; 0在它们的引脚中,这也不会通过检查

答案 1 :(得分:0)

PHP的empty功能将{p> 0视为empty。你可以创建自己的函数,允许0被认为是填充的:

function real_empty($var) { 
    if($var == '0' || !empty($var)) { 
        return false; 
    } else {
        return true; 
    }
}

然后在您的代码中使用real_empty代替empty

演示:https://eval.in/543488