Forum Moderators: coopster

Message Too Old, No Replies

Retrieve data provided at the time of registration

         

krishna

4:48 pm on Jan 25, 2009 (gmt 0)

10+ Year Member



I have a registration script which accepts username, password (un encrypted), Name, School etc...

After registration the user had login i need to show his/her registration details.

Could someone help me (with some basic coding) how can i retrive data.

regards,
krishna

mack

5:56 pm on Jan 25, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I assume you are using a database on backend to store the information?

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.

krishna

5:33 am on Jan 26, 2009 (gmt 0)

10+ Year Member



Thanks Mack for that prompt reply.

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

ergophobe

3:01 am on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



krishna,

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?

krishna

3:54 am on Jan 27, 2009 (gmt 0)

10+ Year Member



$results=(SELECT * from users WHERE userid = '123');
How will i select a username who has just logged in
Suppose a user logs in i need to store that userid and then search with the records in the db.

dreamcatcher

9:41 am on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like what you are after is sessions? You need to store the users username/email address in a session var and query the database based on that? Or something else that is unique to that user. E-mails are generally unique.

mack`s post pretty much covers everything as far as I can see.

dc

ergophobe

4:26 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Instead of
$num_rows = mysql_num_rows($qry);

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)

coopster

7:27 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also, never trust user-supplied information. You should be edit checking your user-supplied data for what you expect, rejecting anything else. And at the very least you should ALWAYS escape the data before using it in a sql statement:
$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'";

No need to keep that extra semicolon in your sql statement either. As a matter of fact it is recommended you do not.