简体   繁体   English

如何获取与登录用户条件匹配的 MySql 数据?

[英]How to fetch MySql data that match the logged in user criteria?

I have the PHP code that will fetch all users listed in the database but I only need the results to list users that have same matching company name as the logged in user.我有 PHP 代码,它将获取数据库中列出的所有用户,但我只需要结果来列出与登录用户具有相同匹配公司名称的用户。 The logged in user can add, edit and delete users of the same company name, in the database the field is labeled "company".登录用户可以添加、编辑和删除具有相同公司名称的用户,在数据库中该字段标记为“公司”。

Here is my code to fetch all users but I need to change so the code only fetches the same "company" as the logged in user.这是我获取所有用户的代码,但我需要更改,因此代码仅获取与登录用户相同的“公司”。

    <?php
                include_once 'connection.php';
                $result = mysqli_query($conn,"SELECT * FROM user_registration");
                ?>

                <?php
                if (mysqli_num_rows($result) > 0) {
                ?>
                  <table class='table table-bordered table-striped'>
                  
                  <tr>
                    <td>First</td>
                    <td>Last</td>
                    <td>Email ID</td>
                    <td>Password</td>
                     <td>Action</td>
                  </tr>
                <?php
                $i=0;
                while($row = mysqli_fetch_array($result)) {
                ?>
                <tr>
                    <td><?php echo $row["first"]; ?></td>
                    <td><?php echo $row["last"]; ?></td>
                     <td><?php echo $row["email"]; ?></td>
                    <td><?php echo ($row["password"])?($row["password"]):('N/A'); ?></td>
                    <td><a href="update.php?id=<?php echo $row["id"]; ?>" title='Update Record'><span class='glyphicon glyphicon-pencil'></span></a>
                    <a href="delete.php?id=<?php echo $row["id"]; ?>" title='Delete Record'><i class='material-icons'><span class='glyphicon glyphicon-trash'></span></a>
                    </td>
                </tr>
                <?php

first you should save the logged-in user data some where.首先,您应该将登录的用户数据保存在某个位置。 you can make session in your login page after user logged-in.您可以在用户登录后在您的登录页面中制作 session。 so do somthing like this in login page:所以在登录页面做这样的事情:

   include_once 'connection.php';
   session_start();
  if(isset($_POST['username']) && isset($_POST['password'])){
         $query = mysqli_query($conn,"SELECT * FROM `YOUR-USER-TABLE` WHERE `username` = '$username' AND `password` = '$passowrd");
        if($row = mysqli_fetch_array($query)){
        $_SESSION['user'] = array(
        "ID" => $row['id'],
        "firstname" => $row['firstname'],
        "lastname" => $row['lasrname'],
        "email" => $row['email'],
        "company" => $row['company']
        );

        }
            
  } else{
      //return user to login page
        exit();
  }
  

then you can use this code:那么您可以使用以下代码:

    include_once 'connection.php';
    session_start();
    $users = array();
    if(isset($_SESSION['user'])){
      $company = $_SESSION['user']['company'];
      $query = mysqli_query($conn,"SELECT * FROM `user_registration` WHERE `company` = '$company'");
    while ($row = mysqli_fetch_array($query)){
        $user= array(
               // get users data for example:
                "ID" => $row['id']
                "firstname" => $row['first'],
                "lastname" => $row['last'],
                "email" => $row['email'],
    //you can get every thing you need. it is not need to get password here

                );
    array_push($users, $user);
    }

    }
    else{
    //do everything that you do with no logged in user in this page
    }

now you can use users array in foreach loop and make your list.现在您可以在 foreach 循环中使用users数组并制作您的列表。 I hope it make it clear for you.我希望它能让你清楚。 if there is something unclear please add comment.如果有不清楚的地方,请添加评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM