PHP - Generating Web Pages According To Database

Associate
Joined
19 Mar 2005
Posts
569
Just wondering if someone could point me in the right direction, I want to have a page which contains links to all of the people that are in my database. Then when you click on their name a new page opens with all of their information.

PHP:
	$query  = "SELECT playerID, playerName FROM players ORDER BY playerName";
	$result = mysql_query($query) or die('Error : ' . mysql_error()); 
	$content =  '<ul>';

	
	while ($row = mysql_fetch_assoc($result)) 
	{


	print "<li><a href=\"playerDetails.php?playerID={$row['playerID']}\">{$row['playerName']}</a></li>\r\n";

	  }
	
	$content .= '</ul>';

This is what I have so far, I want to know if this is correct for what I am doing and what needs to be on playerDetails.php for me to be able to able to show the details of the name just clicked.

Cheers

Leon
 
$playerid = $_GET["playerid"];

(assuming that the playerid field is a unique numeric id for each player, you should validate the URL parameter so that only numeric input is accepted, best way to do this would be to use a regular expression, or something like the is_numeric() function.
example regular expression (matches a string containing 1 or more digits) = /^[\d]+$/

if(preg_match("/^[\d]+$/",$playerid))
{

query db with "SELECT * FROM players WHERE playerID = $playerid"
display relevant fields from database record
}
else
{
echo "invalid player ID number"
}
 
Have done the following below and get "invalid playerID number" . Have I implemented this correctly to get it to display the links to other players? Yes playerID is a unique numeric field which identifies each player. Then on playerDetails.php the field the page that displays the slected users data I just call $_GET["playerid"] in the sql query?
PHP:
		$playerid = $_GET["playerid"];
		
		
		if(preg_match("/^[\d]+$/",$playerid))
	{
	$query  = "SELECT playerID, playerName FROM players WHERE playerID = $playerid";
	$result = mysql_query($query) or die('Error : ' . mysql_error()); 
	$content =  '<ul>';
	while ($row = mysql_fetch_assoc($result)) 
	{
	print "<li><a href=\"playerDetails.php?playerID={$row['playerID']}\">{$row['playerName']}</a></li>\r\n";
	  }	
	$content .= '</ul>';
		}
		else
		{
		echo "invalid player ID number";
		}
 
Back
Top Bottom