嵌套ifs中的语法错误

时间:2012-11-22 12:34:59

标签: php

我确信我的代码中有一个简单的语法错误但我找不到它。这部分代码应该工作的方式是系统检查userID是否存在,它是否存在,或者字段是否为空,它会产生错误。如果没问题,它会转到下一个userName字段并执行相同的检查。它们都报告了之前是否使用了相应的值,但它们都没有报告该字段是否为空。

if($userID != ""){
    $sql = "SELECT * FROM tbl_access WHERE userID = '$userID'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
        if($num_rows == 0){
            $error = "User ID not found, please enter your user ID";
        }
}
else if($userID == ""){
    $error = "Please enter your User ID";
}
else if($username != ""){
    $sql = "SELECT * FROM tbl_access WHERE userName = '$username'";
    $result = mysql_query($sql) or die(mysql_error());
        if(!empty($result)){
            $error = "Username already in use, please select a different username";
        }
}
else if($username == ""){
    $error = "Please enter a username";
}

检查用户ID部分时脚本工作正常,但它完全忽略了用户名部分。我确信这一定是一个简单的语法错误,但我现在已经盯着同一部分代码几个小时了,所以任何帮助都会感激不尽

3 个答案:

答案 0 :(得分:1)

当编写嵌套的if-else语句以在几个替代方案之间进行选择时,使用一些一致的布局,如下所示:

if ( condition1 )
statement1 ;
else if ( condition2 )
statement2 ;
. . .
else if ( condition-n )
statement-n ;
else

陈述-e;

if($userID != ""){
        statements
       if($num_rows == 0){
            $error = "User ID not found, please enter your user ID";
        }
}
else($userID == ""){
    $error = "Please enter your User ID";
}
 if($username != ""){
           statements
           if(!empty($result)){
            $error = "Username already in use, please select a different username";
        }
}
else($username == ""){
    $error = "Please enter a username";
}

试试这个........

答案 1 :(得分:1)

你试图在一个if语句中做太多。请记住,如果您属于if或else if语句中的任何一个,那么整个if语句就结束了。您应该将其分解为较小的语句以实现您的期望。

if($userID != "")
{
    $sql = "SELECT * FROM tbl_access WHERE userID = '$userID'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);

    if($num_rows == 0)
    {
        $error = "User ID not found, please enter your user ID";
    }
}
else
{
    $error = "Please enter your User ID";
}


if($username != "")
{
    $sql = "SELECT * FROM tbl_access WHERE userName = '$username'";
    $result = mysql_query($sql) or die(mysql_error());

    if(!empty($result))
    {
        $error = "Username already in use, please select a different username";
    }
}
else
{
   $error = "Please enter a username";
}

如果您执行上述声明,那么您应该没问题。这将验证两个变量,并根据需要单独执行任一操作。你可以通过这样做来缩短这个:

If(is_null($userID) || is_null($username))
{
    //output your error message here
} else
{
    //you know that both are populated, run your queries now
}

答案 2 :(得分:0)

在第三个if之前删除else:

if($userID != ""){
    $sql = "SELECT * FROM tbl_access WHERE userID = '$userID'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
        if($num_rows == 0){
            $error = "User ID not found, please enter your user ID";
        }
}
else if($userID == ""){
    $error = "Please enter your User ID";
}
if($username != ""){
    $sql = "SELECT * FROM tbl_access WHERE userName = '$username'";
    $result = mysql_query($sql) or die(mysql_error());
        if(!empty($result)){
            $error = "Username already in use, please select a different username";
        }
}
else if($username == ""){
    $error = "Please enter a username";
}