如何从不同的表中获取特定用户的数据

时间:2021-10-19 06:38:46

I want to display the data for user 1 from database A right after he logged in, right now the page showing all the data from the table.

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

currently I have 2 table which is for user login and user transaction. so after they logged in, i want them to be able to view their own record. After do searching, im thinking that it has something to do with session.

目前我有两个表,用于用户登录和用户事务。所以在他们登录后,我希望他们能够查看自己的记录。搜索之后,我想这和会话有关。

can someone help me?

有人能帮助我吗?

connection.php

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);
?>

login.php

login。

<?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

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>
";
}
?>

2 个解决方案

#1


2  

connection.php

connection.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);
?>

login.php

login。

<?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

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>
    ";
}
?>

#2


0  

you can protect and access the user data after they logged in sucessfully by the help of session.

在用户成功登录后,您可以通过会话来保护和访问用户数据。

you could use session_start() for start new session or resume existing session.

可以使用session_start()启动新会话或恢复现有会话。

<?php
session_start();
if(empty($_SESSION['user_sesion_variable']))
  {
    header("location:login.php");
   die();
}
// here go your user database value

#1


2  

connection.php

connection.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);
?>

login.php

login。

<?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

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>
    ";
}
?>

#2


0  

you can protect and access the user data after they logged in sucessfully by the help of session.

在用户成功登录后,您可以通过会话来保护和访问用户数据。

you could use session_start() for start new session or resume existing session.

可以使用session_start()启动新会话或恢复现有会话。

<?php
session_start();
if(empty($_SESSION['user_sesion_variable']))
  {
    header("location:login.php");
   die();
}
// here go your user database value