Pass variable from hyperlink click

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
merchhaus
Forum Newbie
Posts: 9
Joined: Wed Jan 12, 2011 4:41 pm

Pass variable from hyperlink click

Post 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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Pass variable from hyperlink click

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Pass variable from hyperlink click

Post 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']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
merchhaus
Forum Newbie
Posts: 9
Joined: Wed Jan 12, 2011 4:41 pm

Re: Pass variable from hyperlink click

Post 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>");
Post Reply