Welcome to WebmasterWorld Guest from 54.196.125.165

Forum Moderators: open

Message Too Old, No Replies

Template help

Im trying to figure out how to set my template page to get the results

     
6:30 pm on Jan 13, 2010 (gmt 0)

New User

5+ Year Member

joined:Jan 13, 2010
posts: 2
votes: 0


Hi, Im fairly new to mysql and php

I have everything i need figured out apart from one thing

Basically I have my Index.php page and that will display all the results in a list

Jason (ID = 1)
John (ID = 2)
Bob (ID = 3)

This is the link im using
<?php
echo '<a href="template.php?id='.$row['id'].'>'.$row

['name'].'</a>'; ?>

I even have the link set up so that when i click it, it takes me to template.php?id=1 etc

When a user clicks a link (jason for example) I want it to automatically bring up the information from the ID number 1

What im having trouble with is the code on my template page.
I cant seem to figure out how to get it to use the ID number from the address bar and get those results for that ID.

<?php
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry

echo "Name: ".$row['name'];
echo " Age: ".$row['age'];

?>

Any help with this would be much appreciated

Thanks in advance
----
Gorsain

2:09 am on Jan 14, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Welcome aboard gorsain, two things: the first is a small security precaution, the second, a where statement.

Long story short: verify the input from get is actually a number, this will stop attempts to abuse this variable for mySQL injection.

First is not to use the same names for your variables as field names, Hence,

echo '<a href="template.php?rec='.$row['id'].'">'.$row['name'].'</a>';

(Note also you missed the closing quote on the href)


<?php
// presuming you have database connection stuff here
if ($_GET['rec'] > 0) {
// Retrieve just the data for what's in $_GET from the "example" table
$query = "select * from example where id=" . $_GET['rec'];
// No query quotes required, numeric field
$result = mysql_query($query) or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "Name: ".$row['name'];
echo " Age: ".$row['age'];
}
else { echo "Invalid record id supplied"; }
?>
2:54 am on Jan 14, 2010 (gmt 0)

New User

5+ Year Member

joined:Jan 13, 2010
posts:2
votes: 0


Thank you alot, that really helps. got it working perfectly now.

Made my day.

Gorsain

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members