[英]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表:
user
和product
,两个表都包含"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.