Jump to content

How do you add products into the carts table?


shyam13

Recommended Posts

 

Hi All,

 

I am using a construct to create the session to create the cart and add the products to the cart but everytime, I click add to cart, the products are not entered into the cart.

 

this is my code:

 

<?php

 

if (isset($_POST['submit'])){

if (!isset($_SESSION['cart']))

{

$_SESSION['cart'] = array();

}

 

$_SESSION['cart'][] = $_POST['product_id'];

 

exit();

}

?>

 

 

 

Please Help

 

Thank You

Link to comment
Share on other sites

Do you have PHP set to display all errors? If so, are you getting any errors or notices?

 

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

?>

 

 

Have you checked if the variables contain what you expect? To see if the session variable contains anything, for example, you could do something like

 

<?php
//...

    $_SESSION['cart'][] = $_POST['product_id'];

    var_dump($_SESSION['cart']);
    
    exit();

//...
?>

 

 

Have you checked what portions of the script are being executed? For example, to see if the if statements are being evaluated to true, you could try something like

 

<?php

if (isset($_POST['submit'])){
    print 'here - submit';

   if (!isset($_SESSION['cart'])){
        print 'here - session not created yet';

//...
?>

Link to comment
Share on other sites

Do you have session_start at the beginning of that script?

 

Have you tried printing the $_SESSION to see what is in it; at the beginning of the script (before assignment) and at the end of the script (after assignment) and on the next page (which also must have session_start at the beginning)?

Link to comment
Share on other sites

I have started the session and the cart is being updated with the products but I am now trying to display the list of products in my checkout, I am using a foreach statement to retrieve each product from the cart and display it on the checkout.

 

here is my code:

 

<?php foreach ($_SESSION['cart'] as $item)

product::get('product_id' as  $item ); ?>

Link to comment
Share on other sites

You original problem statement was:

 

... the products are not entered into the cart.

 

Now you say:

 

... the cart is being updated with the products

 

So, before we chase another wild goose down the rabbit hole, how about you tell us what you are doing, what is happening, how that is different from what you expect to happen, and show us the code that is not doing what you expect?

 

We are not mind readers and two lines of code does not give us any kind of context to figure out what is wrong.

 

 

Or did I read your last post wrong? Are you saying the problem is solved?

Link to comment
Share on other sites

I am creating a shopping cart, I have got the code to work which adds products to the cart but when I try and show the products in the cart on the check out page, nothing appears.

 

This is the code to add products to the cart:

 

<?php

# this is an mysql select statement to select all the values from the product table where the product vis is true or in computing terms 1.

$product = mysql::select('product', NULL , condition::where('product_vis')->is(1))->execute();

 

# for each product as data place as a row

foreach ($product->data as $row) { ?>

        <!-- Create a new li for each product -->

<li id='<?php echo $row ['product_id']?>'>

           

            <!-- Include a new form for each product so that all the values are encapsulated -->

<form method='post' action='/25.php' class='cart'>

               

                <!-- Hidden form element for the product id -->

                <input name='id' type='hidden' value='<?php echo $row['product_id']; ?>' />

                   

                <!-- Product details -->

<?php echo $row ['product_cost']?>

<?php echo $row['product_name'] ?>

                   

                    <!-- Use editable field for the quantity -->

                    <input name='quantity' type='number' value='1' />

                   

                    <!-- Submit to cart button, not that it doesnt have a name, since its not required -->

                    <input type='submit' value='Add to Cart' />

               

                <!-- Close the cart -->

                </form>

           

            <!-- Close the li -->

            </li>

        <?php } ?>

   

    <!-- Close the ul -->

    </ul>

   

    <!-- Close the form -->

</form>

 

and this is the code to display products on the checkout:

 

><?php foreach ($_SESSION['product_id']

product::get('product_id' as $item);

?>

 

Thank You

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.