Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking to a specific record from a query

Status
Not open for further replies.

OldSmelly

Programmer
Nov 22, 2001
70
NL
Hi All,

I have a Mysql database from wich I can query and put the results on the screen by means of PHP. So far so good :D

But I would like to be able to click on one of those records and go "deeper" into that record. I want tp display in a other screen for example more data from that record. But I dont know how to do this in php. Can anyome please provide me with an example how to do this ? Or is just not posible in php and must I use java(script) or something. I so an example would be greatly apreciated.

Hans Rumpff, The Netherlands
 
let's say you have a table

id |name | address

and id is the primary key.

you display a list of people like this:

Code:
$result= mysql_query("Select id, name from people");
while ($row=mysql_fetch_assoc($result)):
 echo "<a href=\"displaypage.php?id={$row['id']}\">{$row['name']}</a><br/>";
endwhile;

note that each item is included in a link that has the primary key (the id column) as a query attribute

then in displaypage.php
Code:
<?
if (!isset($_GET['id'])) die ("no id attribute");
require_once "dbconnectionscript.php";
$result = mysql_query("Select * from people where id='".mysql_escape_string(trim($_GET['id']))."'");
if (mysql_num_rows() ===0) die ("no record matched that id");
$row = mysql_fetch_assoc($result);
echo <<<EOL
ID = {$row['id']} <br/>
Name = {$row['name']} <br/>
Address = {$row['address']} <br/>

EOL;
?>
 
Ok I hit a snag..

I display my data in a a table so I have addepted the example accordingly (or so I hoped)...

What wrong with this code inside the while loop

$Ret.="<TD> <a href=\"displaypage.php?id={$row['idnr']}\">{$row['achternaam']}</TD>\n>";
 
you have forgotten to close the anchor tag before closing the table cell. you need to include "</a>" before the "</td>
 
OK it's working like a charm....

$Ret .= "<TD><a href=\"displaypage.php?id={$row['idnr']}\">{$row['idnr']}</TD>
<TD>{$row['sexecode']}</TD>
<TD>{$row['voorl']}</TD>
<TD>{$row['voorv']}</TD><TD>{$row['achternaam']}</TD>
<TD>{$row['email']}</TD></a>\n";


But is there a way that the id fiels is not vissible to the user, because the user can modify the ?id and get another record ?
 
you can occlude the idnr by wrapping the link around another field.

but you need to pass some identifier to the server to tell it which record to retrieve. so in that respect you can't completely prevent the user from retrieving other records.

if it is a permissions issue then test the user's access rights against the resource that he/she is requesting.

does the code above really work? i would have thought that the </a> tag should be within the same table cell.
 
Yes it works... But I see what you mean

Have no idea what you mean by a wrap around ?
 
Code:
 $Ret .= "
<TD>{$row['idnr']</TD>                         <TD>{$row['sexecode']}</TD>
<TD>{$row['voorl']}</TD>
<TD>{$row['voorv']}</TD>
<TD><a href=\"displaypage.php?id={$row['idnr']}\">{$row['achternaam']}</a></TD>
<TD>{$row['email']}</TD>\n";
 
What's the diff in your example and my code then except the /a before /td and the a href in a diff place ?
 
I think I'd make the ID field either a hidden field or a Checkbox with no text.
 
@OldSmelly

well ... that is the difference. i'd understood that you wanted to occlude the id number but still retain the linking functionality.

if i was wrong then can you clarify?
 
No that was exactly what I mean but your code didn't change anything I still see the id in the link. Maybe I need new glasses but I don't see anything different in your code except that teh a href part is in a different place...
 
it's not in the link. just in the table. if you want to suppress the id number altogether, delete this bit from the echo statement:
Code:
<TD>{$row['idnr']</TD>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top