gpt4 book ai didi

php - 如何获取特定用户的数据,但来自不同的表

转载 作者:搜寻专家 更新时间:2023-10-30 23:06:09 24 4
gpt4 key购买 nike

我想在他登录后立即显示数据库 A 中用户 1 的数据,现在该页面显示表中的所有数据。

目前我有 2 个表用于用户登录用户交易。所以在他们登录后,我希望他们能够查看自己的记录。经过搜索,我认为它与 session 有关。

有人可以帮助我吗?

connection.php

<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysl_database = "mockup";

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysl_database, $conn);
?>

登录.php

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

if(isset($_POST["submit"])) {
$username = $_POST["username"];
$password = $_POST["password"];

$sql = "SELECT * FROM user
WHERE username='$username' AND password='$password'";
$result = mysql_query($sql);
$numRows = mysql_num_rows($result);
if($numRows==1) {
session_start();
$_SESSION["ID"] = $ID;
header("Location: ./profile_page.php");
} else {
echo "Invalid Login Information";
}
}
?>

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<table>
<tr><td>User Name</td><td><input type="text" name="username" /></td></tr>
<tr><td>Password</td><td><input type="password" name="password" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Login" /></td></tr>
</table>
</form>

profile_page.php

<?php
session_start(); // start the session
include("connection.php");

$ID = $_SESSION["ID"]; // store the user id into session

$sql = "SELECT * FROM transaction WHERE ID='$ID'";
$result = mysql_query($sql);

if($row = mysql_fetch_array($result)) {
$deposit = $row["deposit"];


echo "
<table>
<tr><td>transaction</td><td> : </td><td>$transaction</td></tr>

</table>
";
}
?>

最佳答案

连接.php

<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysl_database = "database_name";

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysl_database, $conn);
?>

登录.php

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

if(isset($_POST["submit"])) {
$username = $_POST["username"];
$pass = $_POST["pass"];

$sql = "SELECT * FROM tbl_user
WHERE username='$username' AND pass='$pass'";
$result = mysql_query($sql);
$numRows = mysql_num_rows($result);
if($numRows==1) {
session_start();
$_SESSION["userid"] = $userid;
header("Location: ./profile_page.php");
} else {
echo "Invalid Login Information";
}
}
?>

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<table>
<tr><td>User Name</td><td><input type="text" name="username" /></td></tr>
<tr><td>Password</td><td><input type="password" name="pass" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Login" /></td></tr>
</table>
</form>

profile_page.php

<?php
session_start(); // start the session
include("connection.php");

$user_id = $_SESSION["userid"]; // store the user id into session

$sql = "SELECT * FROM tbl_user WHERE user_id='$user_id'";
$result = mysql_query($sql);

if($row = mysql_fetch_array($result)) {
$username = $row["username"];
$name = $row["name"];
$email = $row["email"];

echo "
<table>
<tr><td>User Name</td><td> : </td><td>$username</td></tr>
<tr><td>Name</td><td> : </td><td>$name</td></tr>
<tr><td>Email</td><td> : </td><td>$email</td></tr>
</table>
";
}
?>

关于php - 如何获取特定用户的数据,但来自不同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26458117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com