Jump to content

Posting information pulled from Database, one page to another


Ashbridge

Recommended Posts

Hello all,

 

I am trying to create my own Shopping Cart system, very basic but it'll work how I want it too!

 

I have a Product Page which pulls the Product Name, Description, Price and Image. This is working great and the page is styled up and ready! Now, the problem that I am having is somehow storing this information in some kind of session and then displaying the information on the next page which you get to via the "Buy Now" button. This button will take you to the Shopping Cart page where in a table, the information will be displayed.

 

How would I go about this?

Link to comment
Share on other sites

You don't store that data in a session. What you do is only store the product id in the session when the user clicks the add to basket button.

 

Basic example code

<?php
session_start();

// if add to basket button submitted
if(isset($_POST['add_to_basket']))
{
    // make sure product id is numeric value
    if(is_numeric($_POST['product_id']))
    {
        // add product id to basket session variable
        $_SESSION['basket'] = intval($_POST['product_id']);
    }
}

?>

... each product has a add to basket button and a hidden input field containing the product id ...
<form method="post">
    <input type="hidden" name="product_id" value="<?php echo $product_$id ?>" />
    <input type="submit" value="Add To Basket" name="add_to_basket" />
</form>

On your shopping cart page you will run a query to get the product info that match the product id(s) stored in your session. Very basic example code

<?php
session_start();

// check to make sure the session basket exists and is not empty
if(isset($_SESSION['basket']) && !empty($_SESSION['basket']))
{
   // query the products table, return the products matching the ids in the session basket
   $product_ids = implode(',', $_SESSION['basket']);
   $sql = 'SELECT name, description, price, image FROM products WHERE id IN('.$product_ids.')';
   $result = $mysqli->query($sql);

   while($row = $result->fetch_assoc())
   {
       // output product info
   }
}

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.