Page 1 of 1

Pass variable from hyperlink click

Posted: Wed Jan 26, 2011 2:03 pm
by merchhaus
I have a table that pulls all of a users products currently in the database. The user can click on the product name and be taken to an update page where they can update their product. What I need is to create and pass a variable based on what the user clicks so that I bring up the correct item to on the update page. Here is the table with product info and hyperlinks:

Code: Select all

<?php
$query = 'SELECT
		prod_name, short_desc, long_desc, avail_date, country_origin
	FROM
		user_product u JOIN
		product i ON u.prod_id = i.prod_id
	WHERE
		user_id = "' . mysql_real_escape_string($_SESSION['user_id'], $db) . '"';

$result = mysql_query ($query, $db) or die(mysql_error($db));

echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Short Description</th>
<th>Date Available in US</th>
<th>Product Manufactured In</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>";
  echo ("<a href=\"update_product.php\">" . $row['prod_name'] . "</a>");
  echo "</td>";
  echo "<td>" . $row['short_desc'] . "</td>";
  echo "<td>" . $row['avail_date'] . "</td>";
  echo "<td>" . $row['country_origin'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>
 </div>
Most users will have numerous products. When they click on the 'prod_name' I want the update_product.php to pull up the info for only that product. I have the update_product built so I just need to get and pass the variable based on the click.
thanks,
tim

Re: Pass variable from hyperlink click

Posted: Wed Jan 26, 2011 2:08 pm
by danwguy
To the best of my knowledge you can only do that with a submit function. You might be able to get away with something along the lines of... in your link instead of just saying href="update_product.php" do href="update_product.php?prod=whatever". then use that variable that you are passing in the link to do the sql query and display that product only.

Re: Pass variable from hyperlink click

Posted: Wed Jan 26, 2011 3:11 pm
by AbraCadaver
Yes, make sure to select prod_id in your query, then you link will be:

Code: Select all

echo ("<a href=\"update_product.php?prod_id=" . $row['prod_id'] . "\">" . $row['prod_name'] . "</a>");
Then on update_product.php you would get the value with:

Code: Select all

$id = mysql_real_escape_string($_GET['prod_id']);

Re: Pass variable from hyperlink click

Posted: Thu Feb 03, 2011 11:51 am
by merchhaus
Thanks for the responses.

In my query I join tables user_product and product on prod_id. Since both tables have prod_id, do I need to specify u.prod_id in my SELECT and in my link as I've done below?

Code: Select all

$query = 'SELECT
		u.prod_id, prod_name, short_desc, long_desc, avail_date, country_origin
	FROM
		user_product u JOIN
		product i ON u.prod_id = i.prod_id
	WHERE
		user_id = "' . mysql_real_escape_string($_SESSION['user_id'], $db) . '"';

Code: Select all

echo ("<a href=\"update_product.php?prod_id=" . $row['u.prod_id'] . "\">" . $row['prod_name'] . "</a>");