Creating "user pages"
I'm making a very simple web app (for the first time开发者_如何学编程, all this is new to me). I've got a few PHP scripts set up to work with an SQL database to manage user login and registering, but now I want to create something to automatically generate a personal HTML page for a user when they register that displays certain information about them. I'm not sure how to go about doing this.
You need to:
- Create a webpage such as account.php
- Query the database using the usersid which should be stored somewhere like a session or cookie (e.g.
SELECT username, dob, gender FROM users WHERE userid = $_SESSION['userid'];
) - Don't forget to sanitize userid. - You then need to pull this data from the database, depending on what you're using to query the database (PDO etc) then your method will be different (e.g. using mysql_fetch_assoc, pdo's fetch())
- You can then echo the data (e.g.
echo $userData['username']
)
Easiest way:
exmaple.com/user.php?user=123
<?php
$userId = $_GET['user'];
$userInfo = // fetch info from database
?>
<h1>User <?php echo $userInfo['username']; ?></h1>
...
You would create a PHP script that outputs the user's personal HTML page by checking a username passed to the page by a $_GET variable for example:
$username = $_GET['username']
With this username you can add a WHERE clause to your SQL query (don't forget to mysql_real_escape_string($username) before you do that) and retrieve only the info pertaining to that specific user.
Check mysql_fetch_array for a way to retrieve info from the result of a mysql_query.
And the links to a specific user page would look like this:
http://www.youraddress.com/users.php?username=Dylan
精彩评论