homepage Welcome to WebmasterWorld Guest from 23.22.2.150
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

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



 
Msg#: 4060076 posted 6:30 pm on Jan 13, 2010 (gmt 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

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4060076 posted 2:09 am on Jan 14, 2010 (gmt 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"; }
?>

gorsain



 
Msg#: 4060076 posted 2:54 am on Jan 14, 2010 (gmt 0)

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

Made my day.

Gorsain

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved