dangar Posted September 19, 2017 Share Posted September 19, 2017 I have a products page with product images fetched from database using sql and php. I wanted to use php sessions, so when users click on a particular product image, they are directed to the poduct description page for the particular product. With the code below, I can view the products listed in the product page. However, if I click on any product image for their description it opens up the last product's image every time.! Any help would be appreciated. Thanks. <?php include ("connection.php"); session_start(); $query = mysqli_query($conn,"select * from products"); $row = mysqli_fetch_array($query); while($row = mysqli_fetch_array($query)) { $_SESSION['image'] = $row['PRODUCT_IMAGE']; $_SESSION['price'] = $row['PRODUCT_PRICE']; $_SESSION['name'] = $row['PRODUCT_NAME']; $_SESSION['desc'] = $row['PRODUCT_DESCRIPTION']; $_SESSION['outcome'] = $row['PRODUCT_OUTCOME']; $_SESSION['id'] = $row['PRODUCT_ID']; ?> <div class = "product-image-wrapper"> <a href = "Productdetails.php"> <?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($_SESSION['image']).'" alt = "" width = "250px" height ="300px"/>'; ?> </a> <div class = "product-price">$ <?php echo $_SESSION['price']; ?> </div> <div class = "product-name"> <?php echo $_SESSION['name']; ?> </div> <div class = "product-add"> <form class = "form1" method = "post" action = ""> <br> QTY: <input class = "qty" type = "text" name = "qty" value = "1" /> <input type = "hidden" name = "item_number" value = "<?php echo $row["PRODUCT_ID"];?>" /> <input type = "hidden" name = "price" value = "<?php echo $row["PRODUCT_PRICE"]; ?>" /> <input type = "hidden" name = "title" value = "<?php echo $row["PRODUCT_NAME"]; ?>" /> <button class = "btnprimary" type = "submit"> Add To Cart </button> </form> </div> </div> <?php } ?> //In Productdetails.php page, this is how I have tried to use session. <?php session_start(); echo '<img src = "data:image/jpeg;base64,'.base64_encode($_SESSION['image']).'" alt = "" width = "250px" height ="300px"/>'; ?> Link to comment Share on other sites More sharing options...
requinix Posted September 19, 2017 Share Posted September 19, 2017 Don't use a session for this. Sessions are for storing data that lasts while a user visits your website and are not appropriate for passing temporary bits of data between a couple pages. Just use normal variables for displaying the product information, like the $row you already have. Use a query string to pass the product ID to productdetails.php. Link to comment Share on other sites More sharing options...
dangar Posted September 19, 2017 Author Share Posted September 19, 2017 Thanks for the response requinix.However, I am new to php and this is my first project. I understand the logic you've mentioned. Could you demonstrate a part of code for it.? Link to comment Share on other sites More sharing options...
ginerjm Posted September 19, 2017 Share Posted September 19, 2017 If this is your first project and you can't envision the code to accomplish it, perhaps you need to back up and start 'small'. Think about NOT passing the data itself but rather pass the id (ie, key) of the record that has that data. Let the receiving script then do a query with that passed id/key, grabbing the data fields needed and displaying them in your desired format. See how simple it can be when you think it out? Link to comment Share on other sites More sharing options...
cyberRobot Posted September 19, 2017 Share Posted September 19, 2017 Basically, you would have something like this. Note that I changed the SESSION variables to use $row. <?php echo '<a href = "Productdetails.php?id=' . $row['PRODUCT_ID'] . '"><img src = "data:image/jpeg;base64,'.base64_encode($row['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/></a>'; ?> You would then be able to pull the image record using $_GET['PRODUCT_ID'] in Productdetails.php. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.