views:

80

answers:

3

Hello, I already have written a login and register script. The user can login to their account edit their details etc. The details are stored in a users table which has many different data properties related to the user.

I was wondering, how do I make it so they can view their profile and it shows all the data they input. For example, if somebody with username "bob" registered and changed his data, he can click a link "view profile" and he can see his public user profile. I know how to fetch entries and display the data but my problem is I don't understand how to make a profile for my users that can be seen by others and/or themselves. I want these pages to be dynamically created for each user.

Also, if I had a page that retrieved all the recently registered members, it could link to their profile pages.

It is a very common thing across websites and I know there are CMS's that already do this like drupal but I want to code a small controlled solution by myself. Thanks!

A: 

Well, just create a page say userprofile.php that gets a parameter called id (I assume your users have id's .. otherwise have username). If the page is called with no parameter, the page will use the current session user as the profile id and will make all fields editable, otherwise, if it's passed with say "userprofile.php?id=123", then it will display a non-editable version for other members. You can also do a check to see if the session user is the same as the one whose profile is requested, in which case, you want to make the page editable as well.

Rado
I get the first part about creating a page. I have one that dynamically ads information to the database based on the id of the user and I can pull the information in the same way. However, I am not too familiar with how to pass a page in the way you mentioned.I'm guessing "123" is a user id but I'm not sure how you would go about linking in this way. For example, if I retrieve the users from the database, how would I hyperlink their name automatically with that syntax and their ID.Also, is this a safe way to go about this?
+1  A: 

Make your page 'profile.php'

1) Pass id to that page using Get String. Link would be : ....../profile.php?id=3 To learn more about Get and Post Method of form submission. And how PHP handles it ($_POST & $_GET) google it.

2) In that page 'profile.php'

 $id=isset($_GET['id'])?$_GET['id']:-1;
 settype($id , "int");
 if($id<1)
 {
     //wrong id, may be redirect
     header('Location : another-page.php');
     die();
 }
 // use this $id to get information from db and display the information

3) If you want to dynamically tell the person that what his profile link is: Supposing you have $id of that person : $link = '...../profile.php?id=' . $id;

Please consider learning basic php first. Like cookies, get, post, functions, db handling, sessions, string handling, array handling, global variables.

Krishna Kant Sharma
A: 

Are you looking for something like this (an amalgamation of what has been posted and some extra stuff):

 $SQL = "SELECT ID, First_Name, Last_Name, User_Name FROM Users" . 
        "WHERE ID = " . $_GET['id'];

 $con = mysql_connect('server', 'user', 'password');

 $db =  mysql_select_db('database_name', $con);

 $result = mysql_query($SQL);

 while ($db_field = mysql_fetch_assoc($result)) {
      print "User ID: " . $db_field['ID'] . "<BR>";
      print "User Name: " . $db_field['User_Name'] . "<BR>";
      print "First Name: " . $db_field['First_Name'] . "<BR>";
      print "Last Name: " . $db_field['Last_Name'] . "<BR>";
 }

When you want to create a link to their user page, something like this:

 print "<a href=user_profile.php?ID=" . $db_field['ID'] . ">" . $db_field['User_name'] . "</a>";

This takes the ID you pass through from a link on a page, such as http://servername/page/list.php?ID=123 and then the above code retreives whatever information you want (via the SELECT statement) in the database. Then you just lay the information out however you want.

fortheworld
SQL-injection is imminent. You have no chance to survive. Make your time.
Nick Presta
I'm not bothering about SQL injection here. I'm trying to convey an idea to the guy about how to do something.
fortheworld