hi, i am trying to make a simple catalog website without checkout. the user clicks on a product he want and it's displaced on another page...simple. the first page (the one with the product list) has the following code...

<?php
$sql = "SELECT * FROM products";
$result = mysql_query($sql);
if (!$result)
{
echo "Error - query: $sql - " . mysql_error();
exit;
}
						
while ($row = mysql_fetch_array($result)) {
echo "<p class='rc_textAreatext'><a href='viewprod.php?id=" . $row['prod_name']. "'>"
 . $row['prod_name'] . "</a> - " . $row['description'] . 
"&nbsp;&nbsp;&nbsp;$" .$row['price'] . "</p>";
}
?>

on the next page (viewprod.php) i have no idea what to put on the page to call up the product clicked. i know i read it in my books but i cant find it anywhere...can someone help?
Thanks

p.s. sorry if this is a dumb question

Place a hidden field in your script relating to the item in the record set that you are calling, and use something like this:

<a href="viewprod.php?ID=<?php echo $rs['ID']; ?>"target="_blank"><strong>View</strong></a>

on your viewprod.php get the value of the item passed in the URL

$colname_rs_ID = "-1";
if (isset($_GET['ID'])) {
  $colname_rs_ID = (get_magic_quotes_gpc()) ? $_GET['ID'] : addslashes($_GET['ID']);
}
mysql_select_db($database, $conn);
$query_rs_ID = sprintf("SELECT * FROM your_db_table WHERE ID = %s", $colname_rs_ID);
$rs_ID = mysql_query($query_rs_ID, $conn) or die(mysql_error());
$row_rs_ID = mysql_fetch_assoc($r_ID);
$totalRows_rs_ID = mysql_num_rows($rs_ID);

then show the values from the record set on the screen

<input type="hidden" name="ID" value="<?php echo $row_rs_ID['ID']; ?>" />
<?php echo $row_rs_ID['ID']; ?>

ok thanks i'll go try it out

i just got around to using the code. i sorry i dont really understand the code. first, why do you need $colname_rs_ID = "-1"; , why is it -1? And second, arent hidden fields used to get data submitted from a form not to output it?

Thanks again.

just use the get method to retrieve the info from the previous page;

//I suppose you have the connection here....
$id=$_GET['id'];
$sql = "SELECT * FROM products
           WHERE id='$id'";
$result = mysql_query($sql);
if (!$result)
{
echo "Error - query: $sql - " . mysql_error();
exit;
}

$id=mysql_result($result,0,"id");
$name=mysql_result($result,0,"prod_name");
$desc=mysql_result($result,0,"description");
$price=mysql_result($result,0,"price");

echo "ID:".$id."<br>";
echo "NAME:".$name."<br>";
echo "DESCRIPTION:".$desc."<br>";
echo "PRICE:".$price."<br>";
while ($row = mysql_fetch_array($result)) {
echo "<p class='rc_textAreatext'><a href='viewprod.php?id=" . $row['prod_name']. "'>"
 . $row['prod_name'] . "</a> - " . $row['description'] . 
"&nbsp;&nbsp;&nbsp;$" .$row['price'] . "</p>";
}

when you build the link for your product detail page, use $row (or how your id column is named in your DB) instead of $row.

after that, in your product page do:
if(isset($_GET))
{
$id = $_GET;
// show details for this ID
}
else
{
echo "Sorry!!! We don't have this product!"

}

just use the get method to retrieve the info from the previous page;

//I suppose you have the connection here....
$id=$_GET['id'];
$sql = "SELECT * FROM products
           WHERE id='$id'";
$result = mysql_query($sql);
if (!$result)
{
echo "Error - query: $sql - " . mysql_error();
exit;
}

$id=mysql_result($result,0,"id");
$name=mysql_result($result,0,"prod_name");
$desc=mysql_result($result,0,"description");
$price=mysql_result($result,0,"price");

echo "ID:".$id."<br>";
echo "NAME:".$name."<br>";
echo "DESCRIPTION:".$desc."<br>";
echo "PRICE:".$price."<br>";

Looks easy enough. I'm currently reading "Sam's Teach Yourself PHP and Mysql in 24 hour". i'll checkout your code soon. Thanks

@ryan_vietnow
i have tried the code out but i have one question. Whats the point of adding the "0" to ($result,0,"id"); and the other lines? i erased the "0" when i was testing the code and there seemed to be no difference. when i changed the number to 5 or something else i get the folloting error:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 5 on MySQL result index 3 in D:\AppServ\www\Study_Practice\Test_Sites\Shopping Cart\showitem.php on line 22

i'm just wondering is all. from what i get, the point of the "0" is to tell mysql which row to start searching. if i'm mistaken pls correct me

yes,that is correct,"0" is the row(which means first)
where the result of the query will be displaying.

if I put there 1,it means that the data from the 2nd row will be displayed.

ok thanks alot...i guess thats 41 solved threads for you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.