Jump to content

PHP link from database


designaire

Recommended Posts

I'm new to PHP. Thanks for your help in advance.

 

I have a list of id numbers displayed on the page coming from the database. I want to be able to click on an ID number which is a link, and the other information in the database associated with that ID number is displayed

 

0001  I want to click on the ID number and get the information from that number

 

0001

John Smith

47 Smith Lane

New York, NY 45514

Link to comment
Share on other sites

this is going to involve a number of steps,

 

1) if you want the data to just "appear" then you're going to need to use javascript and, more specificly, AJAX.  This isn't the forum to learn that.

 

2) once you pass the value you want to look up to a PHP script (using javascript or through some other means) you can look up the information like this:

 

<?php
$id = $_GET['idNumber'];  //assign your ID number to variable (in this case, $id now equals 00001)
$query = mysql_query("SELECT * FROM databasename.table_name WHERE id = '$id' ");
$result_array = mysql_fetch_assoc($query);

echo $result_array['name_column_in_table'] . "<br>\n";
echo $result_array['address_column_in_table'] . "<br>\n";
echo $result_array['city_column_in_table'].", " . $result_array['state'] . " " . $result_array['zipcode'];

?>

 

3) there's your result, so use javascript or whatever to put it where you want it to "appear"

Link to comment
Share on other sites

pretty simple

 

first question. You want the info to be on the same page or do you want it to go to another page. Either way here is a quick sample

 

<?php
// connect to database below

//query the table
$sql = "SELECT * FROM tablename";
$res = mysql_query($sql) or die(mysql_error());
// loop through the data and display the link
while($r=mysql_fetch_assoc($res)){
// create link to details page
echo "<a href=\"details.php?id=".$r['id']."\">$r['name']</a><br>\n";
}
?>

 

now you will have a link to details.php passing the id number. On details.php you get the id and pass it to the query

<?php
$id = $_GET['id'];
$sql = "SELECT * FROM table WHERE id = '$id'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
echo $r['firstname']." ".$r['lastname'];
?>

 

Hope that helps

 

ray

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.