Forum Moderators: coopster
The first thing you need to do is authenticate the user. From your orrigional post I assume you have already overcome this part, and now want to display the users details from within the database.
The following example is based on php/mysql. What you need to do is read the information that is relevant to the user, retreive this data and then display the data.
Work out the best way to select the data. This will depend on your database structure and how it is pieced together. Lets imagibe you have the followign database set up. I am usign exampel data because i am not sure exactly what info you have.
userid¦firstname¦surname¦age¦zipcode¦phonenumber
The first field is the userid. This field is a unique numerical fiels that increases by 1 every time new data is added. Setting up a userid means we have a field in the database that is totaly unique. There may be users with the same name or same zipcode, but there will never be users with the same userid. This means we can use the userid field to be sure we are extracting data that is specific to only that user.
So here is some code to allow us to select all information we have for user 123
the first thing you need to do is connect to your mysql database from within your php script..
mysql_connect("dbhost", "username", "password")
mysql_select_db("databasename");
we then issue a mysql select query to retreive all information relatign to the userid 123. Basicaly the "*" means all. What we are douign is grabbing
all infoprmation contained within the row that starts with userid 123...
$results=(SELECT * from users WHERE userid = '123');
we now have all the information relating to user123 extracted from the database. We now want to display this information...
while($row = mysql_fetch_array( $result ))
{
echo $row['userid'];
echo"<br>";
echo $row['firstname'];
echo"<br>";
echo $row['surname'];
echo"<br>";
echo $row['age'];
echo"<br>";
echo $row['zipcode'];
echo"<br>";
echo $row['phonenumber'];
{
This will display the data on sepeerate lines. You can place the data within your layout as required. What I generaly tend to do is place the values from the database into an array. This just makes is a lot simpler to work with on the page.
For example if you use the following...
$age = $row['age];
all you need to do is add $age on the page where you want the age to be displayed.
Hope this gives you some ideas.
Mack.
Let me expalin what i exactly need.
For a exam we are registering students. Students can register online. After registration they need to login with the userid and password provided at the time of registration.
Now after loging in they need to print their details and submit manually at the centre.
Here's the login script [All fields are stored in the same table]
<?php
include("config.php");
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
$match = "select id from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."';";
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
exit;
}
else
{
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "you are now logged in!<br>";
}
?>
Mack's script should get you the information you need. Rather than selecting just the userid, you need to select all fields and then you can output all of the user's info on the form so they can print it out.
So what is it that is missing from Mack's answer that you need in order to make your script work?
grab the id by getting the actual result returned by the query and then put it in a session variable
$_SESSION['userid'] = $result['id'];
Now on any future page
$query = "SELECT * from users WHERE id LIKE $_SESSION['userid'];
Now you can get any info you want about the user on any page as long as the session stays active (basically until the user clears cookies or some such action)
$username = mysql_real_escape_string [php.net]($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$result = "SELECT id FROM $table WHERE username = '$username'
AND password = '$password'";