使用多个表查询登录

时间:2014-10-14 02:36:58

标签: php database

我一直想弄清楚如何解决这个问题。我尝试过加入,但结果是错误。我可以把它变成一张桌子,但是学生和教师的桌子上有不同的字段,所以我无法将它们混合起来。某些用户的某些字段为空。我

这是我的表格: 教师,学生

教师的领域是: 用户名 密码 L-NAME FNAME 学科 部门 ACCOUNTTYPE

学生的字段是: 用户名 密码 L-NAME FNAME 年级 部分 联系 生日 监护人 guardiancontact ACCOUNTTYPE

我希望你能给我一个想法。

    <?php

            $username = $_POST['username'];
            $password = $_POST['password'];
            $pos="";

                    $con=mysql_connect("localhost", "john", "Ichthys030313!") or die ("Unable to connect!");
                    mysql_select_db('SoftEng') or die ("Unable to select database!");
                    $sql = mysql_query("SELECT * FROM login WHERE Username = '$username' and Password = '$password'");
                    $count = mysql_num_rows($sql);
                    $row = mysql_fetch_assoc($sql);
                    if($count == 1)
                    {
                            session_start();
                            $_SESSION['userID'] = $row['AccountType'];
                            $pos = $row['AccountType'];
                    }
                            if($pos=="Admin")
                            {
                                    echo "<script type='text/javascript'>
                                    alert ('Welcome, $username')
                                    window.location.href='adminpanel.php';
                                    </script>";
                            }
                            elseif($pos="Faculty")
                            {
                                    echo "<script type='text/javascript'>
                                    alert('Welcome, $username')
                                    window.location.href='list.php';
                                    </script>";
                            }
                            elseif($pos="Student")
                            {
                                    echo "<script type='text/javascript'>
                                    alert('Welcome, $username')
                                    window.location.href='editstudent.php';
                                    <script>";
                            }

                    else{
                                    echo "<script type='text/javascript'>
                                    alert ('Wrong Username or Password')
                                    window.location.href='index.php';
                                    </script>";
                        }
?>

数据库结构:

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

像这样在顶部开始会话。 循环时检查内部的返回值。 使用Double equ for Comparision作为赋值运算符的单个等号。 阅读答案中找到答案的评论

    <?php
 session_start();

            $username = $_POST['username'];
            $password = $_POST['password'];
            $pos="";

                    $con=mysql_connect("localhost", "john", "Ichthys030313!") or die ("Unable to connect!");
                    mysql_select_db('SoftEng') or die ("Unable to select database!");
                    $sql = mysql_query("SELECT * FROM login WHERE Username = '$username' and Password = '$password'");
                    $count = mysql_num_rows($sql);

                    if($count == 1){//Start the iff statment

                        while($row = mysql_fetch_assoc($sql)){//Start the while loop
                    {

                            $_SESSION['userID'] = $row['AccountType'];
                            $pos = $row['AccountType'];
                    }
                            if($pos=="Admin")
                            {
                                    echo "<script type='text/javascript'>
                                    alert ('Welcome, $username')
                                    window.location.href='adminpanel.php';
                                    </script>";
                            }
                            elseif($pos=="Faculty")//Use double equal comparision operator
                            {
                                    echo "<script type='text/javascript'>
                                    alert('Welcome, $username')
                                    window.location.href='list.php';
                                    </script>";
                            }
                            elseif($pos=="Student")//Use double equal comparision operator
                            {
                                    echo "<script type='text/javascript'>
                                    alert('Welcome, $username')
                                    window.location.href='editstudent.php';
                                    <script>";
                            }
                        }

                    } else{
                                    echo "<script type='text/javascript'>
                                    alert ('Wrong Username or Password')
                                    window.location.href='index.php';
                                    </script>";
                        }
?>

**重要说明:** mysql被删除。使用mysqli或PDO

对于Mysqli _功能,请检查此链接http://php.net/manual/en/book.mysqli.php

对于PDO 请检查此链接http://php.net/manual/en/book.pdo.php

如果您想了解运营商,请查看此链接http://php.net/manual/en/language.operators.php