簡體   English   中英

多重條件,如果else語句不起作用

[英]Multiple condition if else statement not working

if(isset($_POST['submitRegister'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];

if(!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
    $error1 = 'Username can only contain: A-Z a-z 0-9 _ - ';

if(!isset($username) || empty($username))
    $error1 = 'Please enter your Username';


if(!preg_match("#[0-9]+#", $password))
    $error2 = 'Password must include at least one number';

if(!isset($password) || empty($password))
    $error2 = 'Please enter your Password';     


if($password != $password2)
    $error3 = 'Passwords do not match';

if(!isset($password2) || empty($password2))
    $error3 = 'Please confirm your Password';


if(!preg_match("#^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}+$#", $email))
    $error4 = 'That e-mail does not appear to be valid';

if(!isset($email) || empty($email))
    $error4 = 'Please enter your E-mail address';       


if(!isset($_POST["terms"]))
    $error5 = 'You must accept the Terms and Conditions';

 else {

    require_once 'server.php';
    require_once 'dbconn.php';

}
}

我在使它正常工作方面遇到了一些麻煩。 如您所見,我有多個if條件。 實際上還有更多內容,但是我對其進行了編輯,以使帖子更短。 我經歷了很多其他的思考,以為我理解了,但顯然不是。

我的問題是,只要不勾選條款復選框(最后一個if語句),一切就可以正常工作。 但是,如果我勾選了該復選框,則無論先前的字段為空還是未正確填寫,它將嘗試連接到數據庫。

考慮我的上一篇文章,我將ifs的順序錯誤(從頭到尾),我將if語句放在條款的前面,並將用戶名放在最后。 我以為這可以解決問題,但只要輸入用戶名,它就會連接,無論其他字段為空。 因此,在這種情況下不起作用。 希望有人能提供幫助,非常感謝。

抱歉,我不太了解您的意圖,但是我可以告訴您,每次您選中條款復選框時,下面的代碼:

 require_once 'server.php';
 require_once 'dbconn.php';

將被執行。

如果要只在沒有錯誤的情況下執行require_once語句,可以這樣:

if (!$error1 && !$error2 && !$error3 && !$error4 && !$error5)
{
    require_once 'server.php';
    require_once 'dbconn.php';
}

我建議您在所有if語句中使用{}塊。

在這種情況下,您不需要“ else”。 此處的多個“ if”語句可以匹配。 最后一個(術語)可以匹配,並且僅當不匹配時,才執行“ else”塊。 您是“ else”塊,是唯一調用您的require_once的部分。

如果您刪除了其他,則應該開始工作。

問題是else語句僅適用於最后一個if(是否檢查條件)。

我建議你做這樣的事情:

if(isset($_POST['submitRegister'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    $email = $_POST['email'];
    $valid = false;

    if(!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
        $error1 = 'Username can only contain: A-Z a-z 0-9 _ - ';

    if(!isset($username) || empty($username))
        $error1 = 'Please enter your Username';


    if(!preg_match("#[0-9]+#", $password))
        $error2 = 'Password must include at least one number';

    if(!isset($password) || empty($password))
        $error2 = 'Please enter your Password';     


    if($password != $password2)
        $error3 = 'Passwords do not match';

    if(!isset($password2) || empty($password2))
        $error3 = 'Please confirm your Password';


    if(!preg_match("#^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}+$#", $email))
        $error4 = 'That e-mail does not appear to be valid';

    if(!isset($email) || empty($email))
        $error4 = 'Please enter your E-mail address';       


    if(!isset($_POST["terms"]))
        $error5 = 'You must accept the Terms and Conditions';

    $valid = !(isset($error1) || isset($error2) || ...); //The number of possible errors

    if($valid)
     {

        require_once 'server.php';
        require_once 'dbconn.php';

    }
}

請注意,保存錯誤的另一種方法是將它們存儲在數組中。

$errors = array();

if(/* Invalid username */)
    $errors[] = 'Invalid username'; //Add new error to the list

/*
...
*/

$valid = count($errors) === 0; //Valid if there is no error

是的,讓我們退后一步,看看語句如何工作。

用偽代碼:

if(statement){
    code1
}

code2

現在,如果statement為true ,則將執行code1,並且將執行code2。 if語句為false ,則code1將不會執行,而code2將無論如何都將執行,因為if語句塊未包含它。 因此,如果我們有以下條件,請將其應用於您的代碼:

if(!isset($email) || empty($email))
    $error4 = 'Please enter your E-mail address';

PHP的工作方式是,它允許您只用一行而不用大括號括起來,但是您可以假設它放在那兒,因此它看起來像這樣:

if(!isset($email) || empty($email)){
    $error4 = 'Please enter your E-mail address';
}

因此出現了問題。 無論if語句的結果如何,它之后的所有代碼仍將執行。 那怎么辦

好吧,你可以做一個很大的嵌套if語句

if(statement1){
    if(statement2){
        if(statement3){
            ...
        }else{
            error
        }
    }else{
        error
    }
}else{
    error
}

但是您可以看到這種情況很快變得很丑陋。

因此,我們可以在多個條件下執行if語句,

if(statement1 and statement2 and statement3 and ...){
    code
}else{
    error
}

但是像您這樣的許多陳述,很快也會變得丑陋。 那怎么辦 使用elseif elseif僅在之前的if語句未執行的情況下執行。

if(!statement1){
    error
}elseif(!statement2){
    error
}elseif(!statement3){
    error
}else{ //meaning no error was triggered
    code
}

另一個解決方案:在大多數語言中,有幾個函數trycatch ,它們被證明是有用的。 看:

try{
    if(!statement1){
        throw exception
    }
    if(!statement2){
        throw exception
    }
    if(!statement3){
        throw exception
    }

    code
}catch(exception){
    throw error because <exception>
}

為什么這有幫助? 如果拋出異常,try塊中的代碼將停止執​​行,並立即轉到catch塊。 這意味着您的所有其余代碼都不會執行(因此,在您的情況下,您要調用的那些錯誤實際上會被調用)。

那么,我的解決方案? 將您的代碼放入一些try-catch塊中

if(!isset($_POST["terms"]))
    $error5 = 'You must accept the Terms and Conditions';

 else {

    require_once 'server.php';
    require_once 'dbconn.php';
}

if ... else塊是成對工作的,在此之前的if語句對else條件沒有任何影響。 從本質上講,這些require_once調用將發生,如果$_POST['terms'] 設置 -不管別的。

我認為您想要的更像是:

if(isset($_POST['submitRegister'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    $email = $_POST['email'];

    //define errors, I'm going to put them into an array 
    // - it makes it easier to evaluate at the end
    $aErrors = array();

    if(!preg_match('#^[a-zA-Z0-9_-]+$#', $username)) {
        $aErrors['error_1'] = 'Username can only contain: A-Z a-z 0-9 _ - ';
    }
    elseif(!isset($username) || empty($username)) {
        $aErrors['error_1'] = 'Please enter your Username';
    }

    if(!preg_match("#[0-9]+#", $password)) {
        $aErrors['error_2'] = 'Password must include at least one number';
    }
    elseif(!isset($password) || empty($password)) {
        $aErrors['error_2'] = 'Please enter your Password';     
    }

    if($password != $password2) {
        $aErrors['error_3'] = 'Passwords do not match';
    }
    elseif(!isset($password2) || empty($password2)) {
        $aErrors['error_3'] = 'Please confirm your Password';
    }

    if(!preg_match("#^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}+$#", $email)) {
        $aErrors['error_4'] = 'That e-mail does not appear to be valid';
    }
    elseif(!isset($email) || empty($email)) {
        $aErrors['error_4'] = 'Please enter your E-mail address';
    }

    if(!isset($_POST["terms"])) {
        $aErrors['error_5'] = 'You must accept the Terms and Conditions';
    }

    //if there are no errors
    if(!$aErrors) {
        require_once 'server.php';
        require_once 'dbconn.php';
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM