简体   繁体   English

如何根据登录用户从 php 中的 MySQL 获取特定用户数据

[英]How to fetch specific user data from MySQL in php, based on logged In User

I'm trying to fetch data of specific (User who is Logged In) User but I'm unable to do that.我正在尝试获取特定(登录的用户)用户的数据,但我无法做到这一点。 I have written a code which can fetch complete data, but I want to fetch data of specific user only.我编写了一个可以获取完整数据的代码,但我只想获取特定用户的数据。

I have tried using WHERE username="$username" and other possible things but none of it worked for me我试过使用WHERE username="$username"和其他可能的东西,但没有一个对我有用

<?php
include_once 'server.php';
$result = mysqli_query($db,"SELECT * FROM users");
?>
<!DOCTYPE html>
<html>
 <head>
 <title> Admin Panel</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 </head>
<body>
<h2>Welcome Admin</h2><hr>

<?php
if (mysqli_num_rows($result) > 0) {
?>
  <table id="students">

  <tr>
    <th>ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Gender</th>
    <th>DateOfBirth</th>
    <th>Contact Number</th>
    <th>Username</th>
    <th>Email id</th>
  </tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
    <td><?php echo $row["id"]; ?></td>
    <td><?php echo $row["fname"]; ?></td>
    <td><?php echo $row["lname"]; ?></td>
    <td><?php echo $row["gender"]; ?></td>
    <td><?php echo $row["dob"]; ?></td>
    <td><?php echo $row["contact"]; ?></td>
    <td><?php echo $row["username"]; ?></td>
    <td><?php echo $row["email"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
 <?php
}
else{
    echo "No result found";
}
?>
    <form  method="GET" action="index.php?logout='1'">
            <button type="submit" class="button" name="logout">Logout</button>
    </form>
 </body>
</html>

and here is the server.php I am able to fetch all user data but I want a specific user data based on who is logged In这是服务器。php 我能够获取所有用户数据,但我想要基于谁登录的特定用户数据

<?php 
    session_start();

    // variable declaration
    $id = "";
    $fname = "";
    $lname = "";
    $gender="";
    $dob = "";
    $contact = "";
    $username = "";
    $email    = "";
    $errors = array(); 
    $_SESSION['success'] = "";

    // connect to database
    $db = mysqli_connect('localhost', 'root', '', 'registration');

    // REGISTER USER
    if (isset($_POST['reg_user'])) {
        // receive all input values from the form
        $fname = mysqli_real_escape_string($db, $_POST['fname']);
        $lname = mysqli_real_escape_string($db, $_POST['lname']);
        $gender = mysqli_real_escape_string($db, $_POST['gender']);
        $dob = mysqli_real_escape_string($db, $_POST['dob']);
        $contact = mysqli_real_escape_string($db, $_POST['contact']);
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $email = mysqli_real_escape_string($db, $_POST['email']);
        $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
        $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

        // form validation: ensure that the form is correctly filled
        if (empty($fname)) { array_push($errors, "Firstname is required"); }
        if (empty($lname)) { array_push($errors, "Lastname is required"); }
        if (empty($dob)) { array_push($errors, "Date of Birth is required"); }
        if (empty($contact)) { array_push($errors, "Contact Number is required"); }
        if (empty($username)) { array_push($errors, "Username is required"); }
        if (empty($email)) { array_push($errors, "Email is required"); }
        if (empty($password_1)) { array_push($errors, "Password is required"); }

        if ($password_1 != $password_2) {
            array_push($errors, "The two passwords do not match");
        }

        // register user if there are no errors in the form
        if (count($errors) == 0) {
            $password = md5($password_1);//encrypt the password before saving in the database
            $query = "INSERT INTO users (fname, lname, gender, dob,contact, username, email, password) 
                      VALUES('$fname', '$lname','$gender', '$dob','$contact', '$username', '$email', '$password')";
            mysqli_query($db, $query);

            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }

    }

    // ... 

    // LOGIN USER
    if (isset($_POST['login_user'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $password = mysqli_real_escape_string($db, $_POST['password']);

        if (empty($username)) {
            array_push($errors, "Username is required");
        }
        if (empty($password)) {
            array_push($errors, "Password is required");
        }

        if (count($errors) == 0) {
            $password = md5($password);
            $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
            $results = mysqli_query($db, $query);

            if (mysqli_num_rows($results) == 1) {
                $_SESSION['username'] = $username;
                $_SESSION['success'] = "You are now logged in";

                header('location: index.php');
            }else {
                array_push($errors, "Wrong username/password combination");
            }
        }
    }

Well you can use session or cookie function in php.那么你可以在 php 中使用 session 或 cookie function。 In your login page enter the command在您的登录页面中输入命令

setcookie('user', $user, time() + (the time for the user must be logged in), '/');

Then in the page where you want to show the user data assign the variable like然后在要显示用户数据的页面中分配变量,例如

$user = $_COOKIE['user'];

and the sql query:和 sql 查询:

select * from table_name where user = $user

I hope this might solve your problem我希望这可以解决你的问题

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

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