Question

I am creating a program in which I have a list of products. I want a user to click on any specific product and see its details. I have written the following code. I am grabbing the current 'product_id' into $_SESSION variable and displaying its details in the next page. Its working,but not giving me the desired output, its grabbing the same id for each product. I think its because i am storing its value in $_SESSION. Kindly check it and tell me the correct way of doing it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

<?php 
session_start();

    include 'connect.php';

        $query=         mysql_query ("select product_id,name, price, description from products" );


        while ($query_array=    mysql_fetch_assoc($query))
        {
            echo '</br><pre>';





            $_SESSION['id']=    $query_array['product_id'];
            $product_id=     $_SESSION['id'];           

            echo "<a href='single_product.php?product_id=$product_id' >";
            print_r ($query_array['name']);
            echo    "</a>";

            print_r ($query_array['price']);

            echo "<img src= 'all_images.php' alt= 'Image'";

            echo '</pre>';


        }

?>
Was it helpful?

Solution

you need to define the $row in $_session and not $_session to $row

     $_SESSION['id']=    $query_array['product_id'];   //<---- must reverse it.
        $product_id=     $_SESSION['id']; 

try this

       $product_id = $query_array['product_id'] ;
       $product_id = $_SESSION['id'];

Or you can pass the ID via $_GET

in your code

   echo "<a href='single_product.php?product_id=$product_id' >";

you can get the product_id like that in the other file

     echo $_GET['product_id'] ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top