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 ha
Solution 1:
Create members table in your database
CREATETABLE `members` (
`id` int(11) NOTNULL AUTO_INCREMENT,
`username` char(20) NOTNULL,
`name` char(30) NOTNULL,
`email` char(40) NOTNULL,
`pass` char(20) NOTNULL,
PRIMARY KEY (`id`)
);
Add an user into table members
INSERTINTO `members` VALUES ('1', 'user1', 'mr. x', 'email@domain.com', '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
<?phpinclude("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";
}
}
?><formaction="<?phpecho$_SERVER['SCRIPT_NAME']; ?>"method="post"><table><tr><td>User Name</td><td><inputtype="text"name="username" /></td></tr><tr><td>Password</td><td><inputtype="password"name="pass" /></td></tr><tr><td></td><td><inputtype="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>
";
}
?>
Solution 2:
You should try yourself first, help is here. database connection with select statement and print first name http://www.w3schools.com/php/php_mysql_select.asphttp://www.tutorialspoint.com/mysql/mysql-select-query.htm
Solution 3:
Assuming your user has a user id (uid), you should be able to fetch information from the database, relative to their said 'uid'.
An example query would look something like:
SELECT firstname, lastname, email FROM `users` WHERE `id` ='UID_HERE';
If your table had firstname, lastname & email fields/columns in it.
Definitely look into using Mysqli to do all database work, because mysql_ is no longer being developed.
Solution 4:
Modify your query
$sql = 'SELECT id_user, email, name, usernameFROM members';
to
$sql = "SELECT id_user, email, name, username
FROM members where id_user = '$dbuser'";
Post a Comment for "Displaying Mysql Data To Clients Once They Log In"