登录代码返回索引页而不是向前移动

时间:2017-12-04 08:12:17

标签: php html mysql

我希望当我选择管理员并输入用户名和密码时,它应该转到admin.html,如果我输入学生或教师的详细信息,则分别转到student.html和teacher.html。

我无法在代码中找到问题。没有记录错误,但仍然无法正常工作。数据库已在phpmyadmin中创建,名称为ezgradingsystem,其中有一个名为loginform的表,其中的列名为IDusername,`password'和'键入' (这将区分管理员,教师和学生登录)。

仍然无法正常工作。当我输入用户名和密码时,它会将我带回index.php页面。

这是代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Login Form </title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <link rel="stylesheet" type="text/css" href="styles1.css"
    </head>
    <body>

        <h1>

            <span>E</span>
            <span>Z</span>
            <span> </span>
             <span>G</span>
             <span>R</span>
             <span>A</span>
             <span>D</span>
             <span>I</span>
             <span>N</span>
             <span>G</span>
             <span> </span>
             <span>S</span>
             <span>Y</span>
             <span>S</span>
             <span>T</span>
             <span>E</span>
             <span>M</span>
              </h1>

        <div class="loginBox"> 
            <img src="user.png" alt="user" class="user"><br>
            <br>
            <br>
            <br>
                    <h2 >Log in Here</h2>
            <form  method="POST" >

                                <select name = "type">
                                <option value="-1">Login As
                                </option>
                                <option value="Admin">Admin</option>
                                <option value="Student">Student</option>
                                <option value="Teacher">Teacher</option>
                                </select></td>/


                <p>Username</p>
                <input type="text" name="username" placeholder="Enter your username here">
                <p>Password</p>
                <input type="password" name="password" placeholder="Enter password here">
                <input type="submit" name="" value="Sign In">



            </form>
                        <br><br><br>



                            </div>

    </body>
    </html>
<?php
$con =mysql_connect("localhost","root","");
if(!$con)
{
    echo "unable to establish connection".mysql_error();
}
$db=mysql_select_db("ezgradingsystem",$con);
if(!$db)
{
    echo "Database not found".mysql_error();
}

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

         $type=$_POST['type'];
         $username=$_POST['username'];
         $password=$_POST['pwd'];
         $query="select * from loginform where username='$username' and password ='$password'and type='$type'";
         $result=mysql_query($query);
         while($row=mysql_fetch_array($result))
         {
             if($row['username']==$username && $row['password']==$password && $row['type']=='admin')
             {

                 header("Location: admin.html");
             }
             elseif($row['username'] ==$username && $row['password']==$password && row['type']=='student')

                 {

                 header("Location: student.html");
             }
             elseif($row['username']==$username && $row['password']==$password && row['type']=='teacher')

                 {

                 header("Location: teacher.html");
             }
         }
     }

    ?>

1 个答案:

答案 0 :(得分:0)

也许你在if上使用你的类型作为条件,试试这个     `

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

     $type=$_POST['type'];
     $username=$_POST['username'];
     $password=$_POST['password'];
     $query="select * from loginform where username='$username' and password ='$password'and type='$type'";
     $result=mysql_query($query);
     while($row=mysql_fetch_array($result))
     {
         if($type=='Admin')
         {

             header("Location: admin.html");
         }
         elseif($type=='Student')

             {

             header("Location: student.html");
         }
         elseif($type=='Teacher')

             {

             header("Location: teacher.html");
         }
     }
 }

?>`