Page 1 of 1

Passing id using an anchor

Posted: Sat Feb 28, 2004 8:23 am
by Rmias
Hi,

In the past you guys help me a lot and I hope someone will give me a solution to this problem. I have a problem passing variables from one php page to another. What I would like to do is, display data from the database as a table and create an anchor for each table data so that if I click on one table data it will pass the id to another page.

Here is the code

Code: Select all

<?php



	$dbc = mysql_connect("localhost") or die ("Unable to connect to databse.");

	$db = mysql_select_db("mydatabase") or die ("Unable to select database.");

	$query = "SELECT asset.asset_id, asset.description, asset.asset_type, department.dept_name, department.asset_owner  FROM asset, department WHERE asset.department_num = department.dept_id";
	
	$result = mysql_query($query, $dbc) or die (mysql_error(). '<br> SQL: '.$sql);
	
		
print "<center>";

print "<table border=1><tr><th>Asset ID</th><th>Asset Name</th><th>Type</th><th>Asset Location</th><th>Owner's Name</th></tr>";
$i = 0;
while ($row = mysql_fetch_row($result))
&#123;
   
   print "<tr bgcolor='#F5EA9E' onmouseover=this.style.backgroundColor='#3165CE'; onmouseout=this.style.backgroundColor='#F5EA9E';>";
   foreach ($row as $field)
   &#123;
      $asset_id = mysql_result($result,i,"asset_id");
      print "<td ><a href = 'editasset.php?id=$row&#1111;$asset_id]'>$field</a></td>";
   &#125;
   print "</tr>";
&#125;
$i++;
print "</table>";

?>
Please help

Posted: Sun Feb 29, 2004 12:12 am
by scorphus
The script for loading from the db and listing each asset is this you posted. Just some corrections:

Code: Select all

print "<td ><a href="editasset.php?id={$row[$asset_id]}">$field</a></td>";
{ and } for the string to be parsed. Also HTML standars recommends you to use double quotes (") for tag attributes.

Now in editasset.php you can do something like:

Code: Select all

$asset_id = $_GET['id'];
Some links:
* http://www.php.net/variables.predefined
* http://www.php.net/language.types.strin ... ng.parsing
* http://www.php.net/language.types.strin ... tax.double

Scorphus.