hedonomania Posted January 29, 2014 Share Posted January 29, 2014 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! Quote Link to comment Share on other sites More sharing options...
.josh Posted January 29, 2014 Share Posted January 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 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>"; Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 29, 2014 Share Posted January 29, 2014 (edited) 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 January 29, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted January 29, 2014 Share Posted January 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 (edited) 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 January 29, 2014 by hedonomania Quote Link to comment Share on other sites More sharing options...
.josh Posted January 29, 2014 Share Posted January 29, 2014 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'] Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 Oh, it's working! Thank you soooo much! Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 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) Quote Link to comment Share on other sites More sharing options...
.josh Posted January 29, 2014 Share Posted January 29, 2014 $_POST != $_GET. Query string parameters are put into $_GET. Sidenote: You should validate or sanitize the value before putting it into your query. At a minimum, do this: $id = (int) $_GET['id']; Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 Ok, I need to remember about that, but it stil won't work! I don't know whether it makes any difference, but i'm doing my database in phpMyAdmin and the ID column is auto-incremented. Quote Link to comment Share on other sites More sharing options...
.josh Posted January 29, 2014 Share Posted January 29, 2014 so is the correct ID showing up in the URL when the page is loaded? Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted January 29, 2014 Solution Share Posted January 29, 2014 (edited) 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 January 29, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 yes, it is. Quote Link to comment Share on other sites More sharing options...
hedonomania Posted January 29, 2014 Author Share Posted January 29, 2014 Thanks! That's it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.