问题描述:

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!

网友答案:

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.

网友答案:

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.

网友答案:

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.

相关阅读:
Top