PHP代码中的Else语句未处理


Else statement in PHP code not processing

我的IF语句在这段代码中工作,但ELSE语句从不处理…我不知道为什么....事实上,所有的代码处理直到ELSE语句,并且captcha工作正常,如果captcha是正确的,用户得到一个积极的消息,数据被发布到数据库。如果验证码错误,则不会发布任何信息,但也不会给出任何消息…请帮助:

if(isset($_POST["captcha"]))
    if($_SESSION["captcha"] == strtolower($_POST["captcha"]))
        if(mysql_query($sql)) {
            echo "<script type='text/javascript'>alert('submitted successfully, The records manager will provide you with your record within 3 days!')</script>";
            //mail('joe.blow@idaho.com', 'SRRS - New Records Await Processing', 'SRRS - There are new records for processing in the SRRS System' );
            //$to  = 'joe.blow2@idaho.com' . ', ';
            //$to .= $email;
            $to = "joe.blow3@idaho.com";
            $subject = "SRRS NOTIFICATION - New Student Record Await Processing";
            $message = "New Student Record Requests have been submitted and Await Processing";
            $from = "joe.blow@idaho.com";
            $headers = "From:" . $from;
            mail($to,$subject,$message,$headers);
            echo "<script type='text/javascript'>alert('An Email Has to sent from the SRRS Records Management System to the Records Management Administrator for Processing.')</script>";
             //Mail the user
                        $to = $email;
            $subject = "SRRS NOTIFICATION - Your new record request has been submitted.";
            $message = "SRRS - Your new record request for $givenname, $legname has been submitted, It will be procssed within 3 working days";
            $from = "joe.blow@idaho.com";
            $headers = "From:" . $from;
            mail($to,$subject,$message,$headers);
        }
        else
        {
            echo "<script type='text/javascript'>alert('Human Verification not entered properly!')</script>";
        }

问题是你的"人工验证"的输出是不正确的是你的SQL查询的else语句而不是之前的if语句进行验证码比较。

你正在做的事情的浓缩版:

if(isset($_POST["captcha"]))
    if($_SESSION["captcha"] == strtolower($_POST["captcha"]))
        if(mysql_query($sql)) {
            // do stuff
        } else {
            // output CAPTCHA ERROR! <-- wrong place
        }

修改语句的位置:

if(isset($_POST["captcha"])) {
    if($_SESSION["captcha"] == strtolower($_POST["captcha"])) {
        if(mysql_query($sql)) {
            // do stuff
        } else {
            // CAPTCHA was fine, but the SQL query failed.
        }
    } else {
        echo "<script type='text/javascript'>alert('Human Verification not entered properly!')</script>";
    }
}

注意:当你只有一个语句(一个if块算作一个语句)时,使用花括号来控制结构是不必要的,如果你包括它们(所以我已经为你添加了它们),它的可读性会好得多。

展望未来:比起大量嵌套的if语句,有更好的方法来编写代码。您应该尝试使用一种结构,在错误发生时捕获并处理错误,而不是将大块代码包装在if语句中,并在最后处理替代方案。试试这样做:

if(!isset($_POST['captcha']) || $_SESSION['captcha'] != strtolower($_POST['captcha'])) {
    echo "<script type='text/javascript'>alert('Human Verification not entered properly!')</script>";
    exit; // kill the rest of the execution
}
if(!mysql_query($sql)) {
    // SQL query failed, output an error
    exit; // kill the rest of the execution
}
// Everything's fine, do the rest of your stuff here.

这可以通过使用函数来进一步优化,并从各种级别的函数返回false而不是exit,当你发现错误时调用它们。

最后,我建议像这样输出带有警告的Javascript可能不是最好的方法。您应该有一种结构,其中执行任务的脚本/函数返回一个布尔值结果(true/false),表示是否一切顺利,可能伴随着错误消息来描述它,并且您应该有一个单独的脚本/函数来处理该结果的表示。这里有一个简单的例子,将结果和消息设置为会话,并使用PHP将用户重定向到显示结果的页面,而不是使用带有警告的脚本标记。

当我也在它- mysql_*函数是不赞成的。您应该使用mysqli_*或PDO代替。