Jump to content

Datbase output further data retriving


hedonomania
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi guys, I would like to ask for some help concerning the database output (mysql and php used).

After I get the database output displayed, that is e.g. I have fields like ID (primary key), Name, Surname I would like to give the ability to further click on the result. E.g. you click on the Name that is the database output and it redirects you to the next page where your surname is displayed.

Any ideas on how to do that? I already have a hyperlink under the result, I want just to somehowly get the information about what Name from the list was clicked, so i can display the appropriate surname on the next page. Thanks a lot!

Link to comment
Share on other sites

when you are looping through and outputting the values, wrap the name in a link that has the id (primary key) appended to the details page (you will need to add it to your select statement if you don't already have it in there)

 

pseudo-code:

while ( $row = fetch row ) {
  echo "<a href='details.php?id={$row['id']}'>{$row['username']}</a><br/>";
}
So then it will output for example:

 

<a href='details.php?id=1'>John</a>
<a href='details.php?id=2'>Jane</a>
<a href='details.php?id=3'>Jim</a>
etc..
Then on your details.php page, grab $_GET['id'] and use that in db query to select the info where id equal that id.
Link to comment
Share on other sites

Thats fine you can still pass in the id as part of the link. The querystring (the text after ?) in a link can pass more than one piece of info.

 

Could you tell us what $row[1], and $row[2] are and the name of php page you want to display the surname on.

Edited by Ch0cu3r
Link to comment
Share on other sites

row[1] is the first field in the database, so the 'Link' field which has the address to the link (e.g. surname.php) that it should redirect you to, after you click row[2] which is the 'Name' field. I want to get on the other page surname.php the field no.3 ($row[3]) to be displayed.

Link to comment
Share on other sites

The thing is my link is also a database output and then it won't work... It looks like:

 

 print "<td><a href=".$row[1].">$row[2]</a></td>";

Okay, so what are you saying then.. that you want to click on the name and it go to a details page, but the name ($row[2]) is already a link pointing to somewhere else? Well what is it pointing to? Is it pointing to the same page $row[1] that you want the details to show up on, or a different page? If it's the former, then it's easy to append the id to it. If it's the latter, well then yeah, that's a problem. You can't have a link that points to 2 places. You'll have to apply the concept to some other piece of text or make a new button altogether.

Link to comment
Share on other sites

All right, So I'll try to explain it as clearly as possible:

 

The database contains 4 fields: (0)ID, (1)Link, (2)Name, (3)Surname

 

1. You are searching within the database

2. You have the output from the database as a list of IDs and Names

3. You can click on the Name of each result and it will redirect you to the next site (following the (1)Link)

4. On the next site you have the surname of the clicked Name displayed

 

I hope my question is clearer now :)

Edited by hedonomania
Link to comment
Share on other sites

Okay so you're saying the target page $row[1] is the next page.

 

So then it's easy enough to append the id to it. Just do this:

 

$target  = $row[1];
$target .= (substr($target,-1) == '?') ? '&' : '?';
$target .= 'id='.$row[0];
print "<td><a href=".$target.">$row[2]</a></td>";
So that will pass the id to the target page. Then on the target page, grab that id and select surname where id=$_GET['id']
Link to comment
Share on other sites

Ok, my problems seems to have continuation... I am trying to display the output from the database by the retrieved id (retriving id works fine) but i'm getting no results from the database... any clue why?
 
 here's my code:
 
$id =$_POST['id'];

$mydb=mysql_connect('localhost',$username, $password) or die("Can not connect to the database");
mysql_select_db($username,$mydb) or die("Can not select the database");

$query_string="SELECT * FROM $tablename WHERE ID='$id'";
$result_id=mysql_query($query_string,$mydb);

$column_count=mysql_num_fields($result_id);

$row=mysql_fetch_row($result_id);

while ($row=mysql_fetch_row($result_id))
 {
 print "<td>$row[0]</td>";
 print "<td>$row[1]</td>";
 print "<td>$row[2]</td>";
 
 print "</tr>\n";
 }
 print "</table>\n";

 mysql_free_result($result_id)

Link to comment
Share on other sites

  • Solution

You seem to be calling mysql_fetch_row twice

$row=mysql_fetch_row($result_id);

while ($row=mysql_fetch_row($result_id))

Remove the while loop so you code is

$row=mysql_fetch_row($result_id);

 print "<td>$row[0]</td>";
 print "<td>$row[1]</td>";
 print "<td>$row[2]</td>";
 
 print "</tr>\n";

While loop is only necessary if your query returns more than one row.

Edited by Ch0cu3r
Link to comment
Share on other sites

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.