Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

php - Displaying MySQL data to clients once they log in

I have tried numerous combinations of code to try and display my client data to them on their own profile page once they log in.

This is my first time designing a website and i have never worked with MySQL before but i have developed a quick understanding of how it all works.

I understand that i need to create sessions to 'store' their information when they log in then i can display it as i wish ?

Does anyone have any code that i could use?

My SQL info:

  • db: taxretur_login
  • table: members

aspects i would like to show:

  • name
  • email
  • username

Thanks. Stan.

UPDATE:

This code works and pulls all my table info accross to a users page but i only want data unique to that paticular user:

<?php
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT id_user, email, 
           name, username
    FROM members';

mysql_select_db('taxretur_login');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
echo "User ID :{$row['id_user']}  <br> ".
     "Email: {$row['email']} <br> ".
     "Name: {$row['name']} <br> ".
     "Username : {$row['username']} <br> ".
     "--------------------------------<br>";
}  
echo "Fetched data successfully
";
mysql_close($conn);
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create members table in your database

CREATE TABLE `members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` char(20) NOT NULL,
  `name` char(30) NOT NULL,
  `email` char(40) NOT NULL,
  `pass` char(20) NOT NULL,
  PRIMARY KEY (`id`)
);

Add an user into table members

INSERT INTO `members` VALUES ('1', 'user1', 'mr. x', '[email protected]', '123');

Just change the variable as your mysql user in page connection.php

connection.php

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

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

login.php

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

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

    $sql = "SELECT * FROM members 
            WHERE username='$username' AND pass='$pass'";
    $result = mysql_query($sql);
    $numRows = mysql_num_rows($result);
    if($numRows==1) {
        session_start();
        $_SESSION["username"] = $username;
        header("Location: ./profile.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.php

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

$username = $_SESSION["username"];

$sql = "SELECT * FROM members WHERE username='$username'";
$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>
    ";
}
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...