PHP页面重定向不';t正常工作(基于数据库角色请求)

PHP页面重定向不';t正常工作(基于数据库角色请求),php,html,mysql,Php,Html,Mysql,我已经用php和html编写了我的代码,用于我的最后一个项目,即学院的课程注册系统 我的问题是: 如果我需要为一个用户重定向页面:它可以工作 但是后来,当我使用switch语句通过从MySQL数据库检索数据来根据角色重定向不同的页面时,它就不起作用了 这是我的代码: <?php $host="localhost"; $username="root"; $password=""; $db="login"; mysql_connect($host,$username,$password); m

我已经用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");
    }
}
?>

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

 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')中给出条件


只需用我的代码替换你的代码

“不起作用”-没有帮助或我们可以解决的问题,请澄清。
$role
是一个字符串。您尚未执行SQL语句。您还有两个案例的
2
没有意义。在定义之前,您还可以尝试在
$role
上执行mysql\u查询。。。为什么要连续两次运行同一个查询?还有SQL注入的问题,因为你没有验证或清理你的输入。这与CSS有什么关系?@Helenesh与phpmyadmin有同样多的关系……你需要从那里隔离问题并进行调试。如果你陷入困境,请清楚地解释什么东西不适合你。我建议你读书。另外,一定不要这样做。这是肮脏的,与问题无关。另外,欢迎加入,感谢您的第一次贡献!一定要:)
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;
}