简体   繁体   English

显示来自不同表的具有相同ID的登录用户的数据

[英]Display data for logged in user with same id from different table

I'm self-learning, so pardon my ignorance. 我正在自学,所以请原谅我的无知。

I have 2 SQL tables: user and product , both tables contain "user_id" fields. 我有2个SQL表: userproduct ,两个表都包含"user_id"字段。

I have successfully created a login system that uses email and password fields from the first table (user). 我已经成功创建了一个使用第一个表(用户)中的电子邮件和密码字段的登录系统。

I want to show specific information from the "product" table to the logged-in user. 我想向“登录”用户显示“产品”表中的特定信息。 This information should be identified from the user_id field. 此信息应从user_id字段中识别。 (user with user_id of 1 should see the product with user_id of 1 (用户ID为1的用户应看到用户ID为1的产品

login page has: 登录页面具有:

<?php

session_start();

$message = "";
require_once('account/pdoconnect.php');

    if(isset($_POST["login"])) {
    if (empty($_POST["email"]) || empty($_POST["password"])) {
        $message = '<label>All fields are required</label>';
    }
    else {
        $query = "SELECT * FROM user WHERE email = :email AND password = :password";
        $statement = $pdotestconn->prepare($query);
        $statement->execute(
            array(
                'email' => $_POST['email'],
                'password' => $_POST['password']
    )
        );
        $count = $statement->rowCount();
        if($count > 0) {
            $_SESSION["email"] = $_POST["email"];
            header("location:account/index.php");
        }
        else {
            $message = '<label>Wrong Email or Password</label>';
        }
    }
}
?>

Index page has: 索引页具有:

<?php session_start();
  if(!isset($_SESSION["email"]))
   {
    header("location:../login.php");
    exit;
   }
?>

<?php
 include("pdoconnect.php");

 $id = $_GET['user_id'];
 $result = $pdotestconn->prepare("SELECT * FROM product inner join user on 
 user.user_id = product.user_id");
 $result->execute(array($id));
 $row = $result->fetch(PDO::FETCH_ASSOC);

?>

Where I insert values with: 我在其中插入值的地方:

<?php
 echo $row['amount'];
?>

Problem: I get the same value in the first row (with user_id = 2) for every user logged in 问题:我在登录的每个用户的第一行(user_id = 2)中都得到了相同的值

You don't have any parameter on your query. 您的查询中没有任何参数。

<?php
     include("pdoconnect.php");

     $id = $_GET['user_id'];
     $result = $pdotestconn->prepare("SELECT * FROM product inner join user on 
     user.user_id = product.user_id WHERE product.user_id =:id");   
     $result ->bindParam(':id', $id, PDO::PARAM_INT);          
     $result ->execute();
     $row = $result->fetch(PDO::FETCH_ASSOC);

?>

First it's probably best to store the user id in the session, so in your first source... 首先,最好将用户ID存储在会话中,因此在您的第一个来源中...

    if($count > 0) {
        $row = $statement->fetch(PDO::FETCH_ASSOC);
        $_SESSION["email"] = $_POST["email"];
        $_SESSION["userID"] = $row['ID'];  // Make sure ID is the column name from the user table
        header("location:account/index.php");
    }

then to display the data, fetch the user ID from the session... 然后要显示数据,请从会话中获取用户ID ...

 $id = $_SESSION["userID"];
 $result = $pdotestconn->prepare("SELECT * FROM product
             WHERE product.user_id =:id");             
 $result->execute(['id'=> $id]);
 $row = $result->fetch(PDO::FETCH_ASSOC);

BTW you don't need to join to the user table if the user_id is in the product table 顺便说一句,如果user_id在产品表中,则您无需加入用户表

暂无
暂无

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

相关问题 根据登录用户的ID显示数据库表中的数据 - Displaying data from a database table based on id of user logged in 如何显示当前登录用户的数据 - How to display data from currently logged in user MySQL / PHP:如何将登录的用户ID插入另一个表,该表从用户输入的表单收集数据 - MySQL/PHP: How to insert logged in user id into another table that is gathering data from a form that the user is inputting 如何使用 id laravel 8 显示来自不同表的数据 - how to display data from different table using id laravel 8 如何使用已记录的用户会话ID将数据从一个表获取到另一个表 - How to fetch data from one table to another using the logged user session id 显示特定用户登录的 db 表中的记录 - display records from db table for specific user logged in 如何在一行中获取具有相同 id 但在另一列中具有不同值的数据并在 php 中的表中显示一个月 - How to fetch data with same id in a row but different value in another column and display one month into table in php 如何从不同的表中提取数据并使用php将其显示在同一页面的不同html表中? - how to pull data from different table and display it in different html table on a same page using php? 无法显示已登录的用户ID - Unable to display logged in user's id 抓取当前登录用户的用户ID并将其添加为其他表中的外键值将返回null-MySQL-CodeIgniter - Grabbing user id of current logged user and add it as foreign key value in a different table returns null - MySQL - CodeIgniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM