[英]Fetching the value from db table as per user id
我喜歡根據用戶 ID 從 db 表中的 User_book1、User_book2、User_book3 中獲取值並將其回顯到我的網頁。 我的目標是,一旦用戶登錄到我的網頁,就會顯示不同的用戶 ID。
這是我的示例數據庫:
id | User_name | User_book1 | User_book2 | User_book3
001 | User A | visible | visible | visible
002 | User B | visible | visible | none
003 | User C | visible | none | none
這是我當前的查詢:
<?php
$sql = mysqli_query($mysqli,"SELECT * FROM users") or die("Error fetching data...");
while($rs=mysqli_fetch_array($sql)){
?>
<?php echo'<div class="col-md-4" style="display: '.$rs['User_book1'].'">'; ?>
<?php echo'<div class="col-md-4" style="display: '.$rs['User_book2'].'">'; ?>
<?php echo'<div class="col-md-4" style="display: '.$rs['User_book3'].'">'; ?>
例如
Logged-in User A --> displayed --> book1, book2, book3
Logged-in User B --> displayed --> book1, book2
Logged-in User C --> displayed --> book1
此查詢中的問題; 每次用戶 B 或用戶 C 登錄時,顯示的書籍都是用戶 A 的。
我正在考慮更改我的查詢代碼並輸入用戶 ID:
<?php
$sql = mysqli_query($mysqli,"SELECT * FROM users WHERE ID = '$id'") or die("Error fetching data...");
while($rs=mysqli_fetch_array($sql)){
?>
但輸出是未定義的變量:id。 伙計們幫我正確查詢這個問題。
是否有任何可能的方法來更改我當前的查詢以實現我的目標,即根據 id 從 db users 中的 User_books 中獲取值?
此處的其他參考是我的登錄會話:
<?php
require('db.php');
session_start();
if (isset($_POST['User_name'])){
$User_name = stripslashes($_REQUEST['User_name']);
$User_name = mysqli_real_escape_string($con,$User_name);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$query = "SELECT * FROM `users` WHERE User_name='$User_name'
and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['User_name'] = $User_name;
header("Location: pages/home-profile.php");
}else{
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>;
}
}else{
?>
登錄會話:
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['User_name'] = $User_name;
$row = mysqli_fetch_array($result)
$_SESSION['id'] = $row['id'];
header("Location: pages/home-profile.php");
現在查詢:
$id = 0;
if(!empty($_SESSION['id'])) {
$id = $_SESSION['id'];
}
$sql = mysqli_query($mysqli,"SELECT * FROM users WHERE id = '$id'") or die("Error fetching data...");
while($rs=mysqli_fetch_array($sql)){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.