php在一个页面上进行多个表单处理

时间:2017-03-02 21:35:33

标签: php mysqli

我已经完成了类似的问题,我认为我已经实现了所有的想法。 我在登录页面上有两个表单,一个用于管理员登录,另一个用于员工登录

第一个表单有一个按钮名称="登录"

第二个表单有一个按钮名称" login2"

管理员的登录工作正常。这是它的代码

 if (isset($_POST['login']))

    { $username = mysqli_real_escape_string($con,$_POST['username']);
      $pwd      = md5($_POST['password']);

      if (isset($username) == true && isset($pwd) == true){

         $login = login($username, $pwd);

        var_dump($login);
        if($login != null) {

            $_SESSION ['user_id']  = $login ['buss_id'];
            $_SESSION ['usernameholder'] = $login ['username'];
            ob_start();

            if ($login['level'] =='1')
                {?> 
                <script>
                        window.location.href = "sadmin/index.php";
                    </script> <?php   } 
                    else if($login['level'] =='2') { ?>
                    <script>
                        window.location.href = "user_admin/index.php";
                    </script>

                    <?php } else if($login['level'] =='3') { ?>
                    <script>
                        window.location.href = "reps/index.php";
                    </script>
                    <?php
                    } /* end of login levels. */


        } /* /if $login !=null */
      } /* /if $login = login */
      } /* / if isset el $_POST */

并且函数login是这个

    function login($username, $password)
{   $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="dbname";
    $con=mysqli_connect($db_host, $db_username,$db_password, $db_name);

    $qry = "SELECT * FROM `businesses` WHERE `username` = '$username' AND `password` = '$password' AND `active` = 1 LIMIT 1";
    $sql = mysqli_query($con,$qry);
    while($row = mysqli_fetch_array($sql))
    {
        return $row;
    } }

我所做的只是简单地复制粘贴登录代码并使其名称如$ username2

$ pwd2等...

并将函数中的查询更改为此

$qry2 = "SELECT * FROM `employees` WHERE `username` = '$username2' AND `password` = '$password2' AND `active` = 1 LIMIT 1";

你可能已经注意到我做了var_dump($ login),所以我做了var_dump($ login2)并且一直返回NULL值。请问我的代码有什么问题?!!!

我要为想看看它的人添加login2代码

if (isset($_POST['login2']))

    { $username2 = mysqli_real_escape_string($con,$_POST['username2']);
      $pwd2      = md5($_POST['password2']);

      if (isset($username2) == true && isset($pwd2) == true){

         $login2 = login_employee($username2, $pwd2);

        var_dump($login2);
        if($login2 != null) {

            $_SESSION ['works_for']  = $login2 ['buss_id_fk'];
            $_SESSION ['emp_id'] = $login2 ['emp_id'];
            $_SESSION ['user_name'] = $login2 ['username'];
            ob_start();

            if ($login_employee['level'] =='1')
                {?> 
                <script>
                        window.location.href = "sadmin/index.php";
                    </script> <?php   } 
                    else if($login_employee['level'] =='2') { ?>
                    <script>
                        window.location.href = "user_admin/index.php";
                    </script>

                    <?php } else if($login_employee['level'] =='3') { ?>
                    <script>
                        window.location.href = "reps/index.php";
                    </script>
                    <?php
                    } /* end of login levels. */


        } /* /if $login !=null */
      } /* /if $login = login */
     } /* / if isset el $_POST */

这是函数login_employee代码

function login_employee($username2, $password2)
{   $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="leadapp";
    $con=mysqli_connect($db_host, $db_username,$db_password, $db_name);

    $qry2 = "SELECT * FROM `employees` WHERE `username` = '$username2' AND `password` = '$password2' AND `active` = 1 LIMIT 1";
    $sql2 = mysqli_query($con,$qry2);
    while($row2 = mysqli_fetch_array($sql2))
    {
        return $row2;
    } }

    ?>

只是为了清除我添加了一个截图,而不是我的表单的代码。 enter image description here

1 个答案:

答案 0 :(得分:1)

让我们来看看login_employee函数的一些内容

 function login_employee($username2, $password2)
{   $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="leadapp";
    $con=mysqli_connect($db_host, $db_username,$db_password, $db_name);
    var_dump($username2);  var_dump($password2); 
    $qry2 = "SELECT * FROM `employees` WHERE `username` = '$username2' AND `password` = '$password2' AND `active` = 1 LIMIT 1";
    $sql2 = mysqli_query($con,$qry2);
    var_dump(mysqli_fetch_array($sql2));
    while($row2 = mysqli_fetch_array($sql2))
    {
        return $row2;
    } }

    ?>

所以前两个var转储你获得名称和密码,之后检查这个值是否真的在db employees

如果是var_dump(mysqli_fetch_array($ sql2));不要返回你的数据库行 - 这意味着你有空,因为数据库中没有匹配行