PHP页面重定向无法正常工作(基于数据库角色请求)

时间:2019-03-07 12:10:07

标签: php html mysql

我已经用php和html编写了我的最终项目的代码,这是拼贴画的课程注册系统。

我的问题是:

如果我需要为一个用户重定向页面:可以。

但是后来,当我使用switch语句通过从MySQL数据库中检索数据来基于角色重定向不同的页面时,它不起作用。

这是我的代码:

<?php
$host="localhost";
$username="root";
$password="";
$db="login";
mysql_connect($host,$username,$password);
mysql_select_db($db);

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

    $username= $_POST['username'];
    $password= $_POST['password'];
    $sql="select * from loginform where username='$username' AND password='$password' ";
    $result=mysql_query($sql);
    $result=mysql_query($role);
    $role="SELECT * FROM loginform  username='$username' AND password='$password'";

    if(mysql_num_rows($result)==1){
        session_start();
        switch($role)
        {
            case 1:
                $_SESSION['role']='admin';
                header("Location: http://localhost/test/form1.html");
                break;
            case 2:
                $_SESSION['role']='faculty';
                header("Location: http://localhost/test/faculty.html");
                break;
            case 2:
                $_SESSION['role']='student';
                header("Location: http://localhost/test/student.html");
                break;
        }


        echo " You Have Successfully Logged in"; 
        exit();

    }

    else{ 

        $message='Worning !!! ...Incorrect UserName or PassWord';
        echo "<script type='text/javascript'>alert('$message');</script>";
        exit();
        header("Location: http://localhost/test/loginn.php");
    }
}
?>

1 个答案:

答案 0 :(得分:-1)

首先在switch语句中使用true,请参阅this了解更多信息

 switch($role)
        {
            case 1:
                $_SESSION['role']='admin';
                header("Location: http://localhost/test/form1.html");
                break;
            case 2:
                $_SESSION['role']='faculty';
                header("Location: http://localhost/test/faculty.html");
                break;
            case 2:
                $_SESSION['role']='student';
                header("Location: http://localhost/test/student.html");
                break;
        }

在此switch语句中,它将始终运行第一个case(case1),因为您没有提供case条件(case $ role =='something')

switch(true)
{
    case $role=='admin':
        header("Location: http://localhost/test/form1.html");
        break;
    case  $role=='faculty':
        header("Location: http://localhost/test/faculty.html");
        break;
    case $role=='student':
        header("Location: http://localhost/test/student.html");
        break;
}

只需用我的代码替换