【问题标题】:Multiple condition if else statement not working多个条件 if else 语句不起作用
【发布时间】:2014-03-21 16:40:05
【问题描述】:
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 else tuts 并认为我理解它,但显然不是。

我的问题是,只要我不勾选术语复选框(最后一个 if 语句),一切正常。但是如果我勾选复选框,它将尝试连接到数据库,无论之前的字段是空的还是没有正确填写。

考虑到我上一篇文章中我的 if 顺序错误(从后到前),我将 if 语句放在最前面,将用户名放在最后。我认为这解决了它,但只要我输入用户名它就会连接,无论其他字段是否为空。所以这在这种情况下不起作用。 希望有人能帮忙,非常感谢。

【问题讨论】:

  • 一目了然,我怀疑你想要elseif 在那里......

标签: php forms if-statement


【解决方案1】:

对不起,我不太明白你的意图,但我可以告诉你,每次你勾选条款复选框时,下一个代码:

 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 语句中使用 {} 块。

【讨论】:

  • 我会选择这个答案,因为它只需要我添加 1 行代码。非常感谢您的帮助。
【解决方案2】:

在最后一种情况下,您不需要“else”。这里的多个“if”语句可以匹配。最后一个(“条款”之一)可以匹配,并且当且仅当它不匹配时,才会执行“else”块。你是“else”块是唯一调用你的 require_once 的部分。

如果删除 else,它应该会开始工作。

【讨论】:

  • 你是对的,删除 else 会使其工作,这意味着它会连接到我的数据库。但即使字段为空,或者如果它包含恶意代码,它也会这样做。
【解决方案3】:

问题是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

【讨论】:

  • 感谢您的回复。我接受第一个建议纯粹是因为它意味着必须编写更少的代码。我总是在帖子中看到您应该编写尽可能少的代码来完成这项工作。此外,我目前正在做数组教程,尽管在这种情况下,我认为它不会减少我必须编写的代码量。非常感谢:)
【解决方案4】:

好吧,让我们退后一步,看看 if 语句是如何工作的。

在伪代码中:

if(statement){
    code1
}

code2

现在,如果语句是true,那么代码1 将执行,代码2 将执行。 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
}

但是有很多像你这样的陈述,也会很快变得丑陋。那怎么办?使用elseifelseif 仅在前一个 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 blocks

【讨论】:

  • 如果“用户在表单中输入垃圾数据”属于特殊情况,那么您必须生活在一个特别田园诗般的世界中 - 对我来说,这更像是日常发生的事情:\跨度>
  • 我喜欢例外。让事情保持整洁,我可以使用错误消息和不同类型错误的代码报告错误。而那个非常特别。
  • 例外是好的 - 同意。我只是不相信 Web 表单上的错误用户输入符合您不愿意(或无法)允许该方法从中恢复的条件(理想情况下,您的应用程序不应该抛出异常 - 这意味着出现问题)。但是,对于最初的问题,在第一个输入错误上抛出异常将不允许记录后续错误并将其报告给用户 - 所有 5 个错误很可能同时发生,因此所有 5 个错误应该同时报告回来。
  • 感谢您的深入回复。这表明,剥猫皮的方法肯定不止一种! elseif 的问题在于它一次只给我错误 1 ​​个字段,这意味着我必须在它告诉我密码错误之前整理用户名中的错误。虽然它有效,但它并不是我所追求的。非常感谢:)
【解决方案5】:
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 条件没有任何影响。从本质上讲,如果$_POST['terms'] 被设置,那些require_once 调用就会发生——不管其他什么。

我认为你想要的更像是:

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

【讨论】:

  • 只是对您的回答表示感谢。我目前正在学习数组教程,因为这是我的项目中肯定需要的。
  • 顺便说一句 - 使用 if(!isset($var) || empty($var)) 绝对没有意义,因为 empty 评估是否设置了变量......只需使用 if(empty($var))
猜你喜欢
  • 2017-07-02
  • 2014-09-06
  • 1970-01-01
  • 2023-03-13
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多