if else php检查几个查询不起作用

时间:2015-05-20 16:34:20

标签: php html mysql sql if-statement

我想这样做,如果它没有在表中找到一行,它会转到下一个查询。第一个'if'($ query1)有效;然而,它停留在($ query2)并停在那里而不是去$ query3。我在if else方法上出错了吗?

$connection = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("testproject", $connection);
    $query1 = mysql_query("SELECT * FROM mentors where username='$username'",$connection);
    $query2 = mysql_query("SELECT * FROM students where username='$username'",$connection);
    $query2 = mysql_query("SELECT * FROM admin where username='$username'",$connection);
    $rows1 = (mysql_num_rows($query1)==1);
    $rows2 = (mysql_num_rows($query2)==1);
    $rows3 = (mysql_num_rows($query3)==1);
    if ($rows1) 
    {
            $query4=mysql_query("UPDATE mentors SET password='$password' WHERE username='$username'",$connection);
            if($query4)
            {
                header("location: ../htmls/main_menu_m.php"); // Redirecting To Other Page
                exit();
            }
    }
    else if(!$rows1)
    {
        if($rows2) 
        {
            $query5=mysql_query("UPDATE students SET password='$password' WHERE username='$username'",$connection);
            if($query5)
            {
                header("location: ../htmls/main_menu_s.php"); // Redirecting To Other Page
                exit();
            }
        }
        else if (!$rows2)
        {
            if ($rows3) 
            {
                $query6=mysql_query("UPDATE admin SET password='$password' WHERE username='$username'",$connection);
                if($query6)
                {
                    header("location: ../htmls/main_menu_a.php"); // Redirecting To Other Page
                    exit();
                }
                else
                {
                    header("location: ../index.php"); // Redirecting To Other Page
                    exit();
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

您没有设置变量$query3

$connection = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("testproject", $connection);
    $query1 = mysql_query("SELECT * FROM mentors where username='$username'",$connection);
    $query2 = mysql_query("SELECT * FROM students where username='$username'",$connection);
    $query3 = mysql_query("SELECT * FROM admin where username='$username'",$connection);
.....

答案 1 :(得分:1)

对我来说似乎有点错字,改变:

$query2 = mysql_query("SELECT * FROM students where username='$username'",$connection);
    $query2 = mysql_query("SELECT * FROM admin where username='$username'",$connection);

为:

$query2 = mysql_query("SELECT * FROM students where username='$username'",$connection);
    $query3 = mysql_query("SELECT * FROM admin where username='$username'",$connection);