Jump to content

Now that I know what to ask, here goes


alwaysinit

Recommended Posts

I have a musician community portal site. I'm trying to create 'one' PHP page that is used for all musician's profiles.

Does anyone know what I should name this page to where it will function as everyone's profile page?(Can it be just "profiles.php"? Or do I need something more in the name to do that?

Thanks again for the help guys
Link to comment
Share on other sites

[code=php:0]
How would the "profile.php" page come to be renamed "profiles.php?member=thorpe". How did it get that way?
[/code]

Either you worded this oddly or you're fairly new.  Its the same page with a different GET value...
Simply by changing memebe= in your address bar, or by using the more sought after <a href..., you can gain the same effect.
Link to comment
Share on other sites

Yes I'm new to coding(two months). Sorry if my questions seem odd and presented incorrectly.

From your responses(tanx to all), this is what I now think:

(1) I create the profiles.php page.
(2) Use "<a href='profiles.php?member=memberName'>memberName</a>" in my site's search results?(I'm not sure where to use the (<a href))
(3) In the "profiles.php" place "$_GET['member']" to show the page as that member's.
(4) then start echoing result rows

Do I have all that right? If incorrect could someone show the correct sequence of events for showing 1000's of different profiles off the same page?
[quote]Here's the basics....

Code:
<a href='profiles.php?member=memberName'>memberName</a>[/quote]  Where does this link go?
Thanks again
Link to comment
Share on other sites

The whole process is database driven. You'll need a database table.

[code]
CREATE TABLE users (
  id INT PRIMARY KEY,
  uname VARCHAR(80),
  hobby TEXT
)
[/code]

This is a make believe example designed to store a users id, name and there hobbies. Lets pretend we had 3 users...

[code]
id name hobby
--------------
1  bob  internet
2  foo    cooking
3  mary  music
[/code]

Ok.... now you'll need 2 pages. One (index.php) to display the links to your users profile, and Two (profiles.php) to display each users hobby in detail.

index.php

[code=php:0]
<?php
  // connect to database
  $sql = "SELECT uname FROM users";
  $result = mysql_query($sql);
  if ($result) {
    if (mysql_num_rows($result) > 0) {
      // here we loop through each member in the database
      while ($row = mysql_fetch_assoc($result)) {
        /// creating a link out of there uname, and passing there uname through the url.
        echo "<a href=\"profiles.php?member={$row['uname']}\">{$row['uname']}</a><br />";
      }
    } else {
      echo "No users found";
  } else {
    echo "Query failed ". mysql_error();
  }
?>
[/code]

profiles.php

[code=php:0]
<?php
  if (isset($_GET['member'])) {
    // connect to database.
    $sql = "SELECT uname, hobby FROM users WHERE uname = '{$_GET['member']}'";
    $result = mysql_query($sql);
    if ($result) {
      if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result));
        echo "The user {$row['uname']} enjoys the hobby {$row['hobby']}";
      } else {
        echo "No user found by that name";
    } else {
      echo "Query failed ".mysql_error();
    }
  } else {
    echo "No member selected";
  }
?>
[/code]

Ive tried to keep these examples pretty simple. There isn't allot of html mixed in just to make things a little clearer. Hope it helps.

ps; Take a look at [url=http://php.net/mysql_connect]mysql_connect[/url]() in the manual for examples on how to connect to the database.
Link to comment
Share on other sites

  • 5 years later...

It's a made-up example to communicate a point. The code is untested and it's not advised to just copy-paste it and put it on your live website due to the gaping security holes in the code.

 

If you take a look at the code for index.php you'll notice a missing } after "No users found!"; The same for profiles.php after "No user found by that name";

Link to comment
Share on other sites

Nitpicking a bit, but I'd written the code a bit differently for ease of readability. I'd check for error conditions first, and if found add a warning/error message and exit early. Now, since this isn't a function or included code, I can't really exit early from this, but that's easily fixable by separating logic and presentation. (Read more about it in the "HEADER ERRORS" thread stickied at the top of this section.)

 

Now, as to how I'd written the code:

function getMemberDetails ($username) {
if (empty ($username)) {
	return "No member selected";
}

// Create the query, and escape input to avoid SQL injections.
$sql = "SELECT uname, hobby FROM users WHERE uname = '%s'";
$sql = sprintf ($sql, mysql_real_escape_string ($username));
$result = mysql_query ($sql);

if (!$result) {
	// TODO: mysql_error () should/must be removed before putting the code into production!
	return "Query failed " . htmlspecialchars (mysql_error ());
}

if (mysql_num_rows ($result) <= 0) {
	return "No user found by that name";
}

return mysql_fetch_assoc ($result);
}

// Retrieve the user details, and check if it returned an array or string (error message).
if ($userDetails = getMemberDetails ($_GET['member'])) {
// Query succeeded, add completed message to output. Adding escaping to prevent HTML injections.
$temp = 'The user %1$s enjoys the hobby %2$s';
$output = sprintf ($temp, htmlspecialchars ($userDetails['uname']), htmlspecialchars ($userDetails['hobby']));
} else {
// Something failed, show error message.
$output = $userDetails;
}

 

Note that I haven't validated $_GET['member'] in the code above, something which you definitely should be doing.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.