Kenny
Kenny

Reputation: 9

Custom pages on php for each row on an SQL table?

How can I have custom pages for each row of an sql table? Without having to manually create each one of course. Say for example I have a table of usernames with their details. I would like it so that theres a unique page for this user that shows all his/her details. This page should be changed whenever the user updates their details of course.

I don't really know what this kind of pages are called and would like links or codes to references on how to achieve this.

Cheers.

Upvotes: 0

Views: 485

Answers (1)

mellamokb
mellamokb

Reputation: 56769

Generally, there is a single page that handles the display of all of the records, using a parameter to identify which record should be shown. The simplest method is through the querystring, so that example.com/userinfo.php?userid=1 would show details for user #1, and example.com/userinfo.php?userid=2 would show details for user #2 etc.

When you are writing the userinfo.php page as above, you can use code like the following to display the correct user:

<?php
// check that user id was passed in query string
if (isset($_GET['userid'])) {
    $userid = (int)$_GET['userid'];
    // create sql query to lookup user details and store
    // in variables like $username, $displayname, etc.
    // AND USE PREPARED STATEMENTS! mind you
    // don't inject the variable into strings and allow SQL injection
} else {
    // display error page that user id is not valid, or default page
    // or redirect to home page, etc.
}

// display user details using HTML from data looked up
?>
<p>Username: <?php =$username ?></p>
<p>Display Name: <?php =$displayname ?></p>
.. etc.

Upvotes: 3

Related Questions