How to retreive specific values from database to display on webpage

i have a register and login page where the users can register and login. their details gets saved in the database. my requirement is that when the user will login to the account, they should be redirected to their profile page, where they can view all their profile details. Till now i have been able to make the user login and dislay a part of their profile page, for this purpose the code that i am using is


<?php
include('retailer_session.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to your homepage</title>
        <meta name="viewport" content="width=device-width", initial-scale=1.0">
        <link href="css/bootstrap.min.css" rel="stylesheet" />
        <link href="css/styles.css" rel="stylesheet" />
        <link href="css/carousel.css" rel="stylesheet">
</head>
<body>
<div id="profile">
    <div class="navbar navbar-inverse navbar-static-top">
        <div class="container">
            <a href="#" class = "navbar-brand"> <id="welcome">Welcome : <i><?php echo $login_session; ?></i> </a>
                <button class = "navbar-toggle" data-toggle = "collapse" data-target = ".navHeaderCollapse">
                    <span class = "icon-bar"> </span>
                    <span class = "icon-bar"> </span>
                    <span class = "icon-bar"> </span>
                </button>
            <div class="collapse navbar-collapse navHeaderCollapse">
                <ul class = "nav navbar-nav navbar-right">
                    <li class ="active"> <a href="admin_profile.php">Home</a></li>
                    <li> <a href="retailer_logout.php"><id="logout">Log Out</a></li>
                </ul>
            </div>
        </div>     
    </div>
</div>
</body>
</html>

Apart from this i want a listing where the user can view their data and edit their details as it is done on many websites. can anyone please help me with the code

Hi that is not a lot information to build on. You have to provide some more information, like database information etc…

I hope I’m understanding your question correctly. To display a user’s data, you’ll have to have a query to the database, get the user’s info, put it into PHP variables, and then echo it in your HTML where you want it to appear. I worried about understanding your question because, if you’re saving user info to the database, you already know about queries.

So let’s say you put a link on this page to user_profile.php. Since you have the user’s ID, probably stored in $Login_session, you need to include that ID in your user_profile.php link. Your link would read like this:

<a href="user_profile.php?id=<?php echo $user_id; ?>">

The actual link will be something like

user_profile.php?id=123

When you put something after a question mark in a URL, that info will be passed to the page via the $_GET global array.Then, over in user_profile.php, you would have something like this:

mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$query = 'SELECT * FROM users WHERE id = \'' . $_GET['id'] . '\'';
$result = mysql_query($query);
$user_info = mysql_fetch_assoc($result);

Starting from the first line, you connect to MySQL, select the database you want, form a query string, execute the query, and then put the results into an associative array, where the keys will be the field names and the values will be the field values.

$user_info should wind up looking something like this:

$user_info Array
  'user_id' => 123
  'name'    => 'John Smith'
  'email'   => 'jsmith@example.com'

Then you can echo that info in your HTML.

The above is not code you can past in and get to work. Your question, if I understood it correctly, requires a lot more answer than we can give here. I would recommend googling for “php mysql tutorial.” Also, look for Kevin Yank’s book on PHP and MySQL. I think the free chapters are enough to get you started. Good luck! :smiley:

You need to be aware that the old mysql_* functions are deprecated as of version 5.5 of PHP and will likely be removed from the next version of PHP. You should now be using either the mysqli_* extension or PDO.

Also if you’re letting user submitted data anywhere near the database without first sanitizing it, then at the least escaping it then you leave your script wide open to potential SQL injection attack. Once you’ve migrated your code to use either the mysqli_* extension or PDO you should be making use of prepared statements. PDO is better as it allows the use of named parameters.