PHP: shopping cart empty after clicking the add button

As per title, I have a problem when adding products to the cart: it would show a window message that says that the product has been added, but in truth it is not there. It gives the following error: Fatal error: Cannot use object of type stdClass as array .

La linea di codice e’:

<?php echo $value['item_name']; ?>

Here is the code file reserve.php :

[php]

<?php session_start(); ini_set('display_errors', 1); $connect = mysqli_connect('127.0.0.1', 'root', '***********', 'Community Garden List'); if (isset($_POST['add'])) { if (isset($_SESSION['cart'])) { $item_array_id = array_column($_SESSION['cart'], 'product_id'); if (!in_array($_GET['id'], $item_array_id)) { $count = count($_SESSION['cart']); $item_array = array( 'product_id' => $_GET['id'], 'item_name' => $_POST['hidden_name'], 'product_price' => $_POST['hidden_price'], 'item_quantity' => $_POST['quantity'], ); $_SESSION['cart'][$count] = $item_array; echo ''; } else { echo ''; echo ''; } } else { $item_array = array( 'product_id' => $_GET['id'], 'item_name' => $_POST['hidden_name'], 'product_price' => $_POST['hidden_price'], 'item_quantity' => $_POST['quantity'], ); $_SESSION['cart'][0] = $item_array; } } if (isset($_GET['action'])) { if ($_GET['action'] == 'delete') { foreach ($_SESSION['cart'] as $keys => $value) { if ($value['product_id'] == $_GET['id']) { unset($_SESSION['cart'][$keys]); echo ''; echo ''; } } } } ?>

?>[/php]

html code

[php]<?php

$query = ‘SELECT * FROM product ORDER BY serial ASC’;
$result = mysqli_query($connect, $query);

if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
?>



           <img src="<?php echo $row['image']; ?>" class="img-responsive" style="width:100%;>
       <h5 class="text-info"><?php echo $row['pname']; ?></h5>
       <h5 class="text-danger">€ <?php echo $row['price']; ?></h5>
       <h5 class="text-info"><?php echo $row['pdescription']; ?></h5>
       <input type="text" name="quantity" class="form-control" value="1">
       <input type="hidden" name="hidden_name" value="<?php echo $row['pname']; ?>">
       <input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>">
       <input type="hidden" name="hidden_pdescription" value="<?php echo $row['pdescription']; ?>">
       <input type="submit" name="add" style="margin-top:5px;" class="btn btn-success" value="Add to Bag">
       </div>
       </form>
       </div>
       <?php

   }

[/php]
I’ve tried print “

”; var_dump($row); exit; 
after this line: foreach e($_SESSION[‘cart’] as $key => $value) { and it comes a table with NULL inside. What does that mean?

Before that, i tried to change $value[‘item_name’] with $value->item_name , and i got the following error:
Notice: Undefined property: stdClass::$item_name in

Will you please help me to understand what’s wrong?thank you.

sorry, this is the last part of the code:

}
}
?>

The above brackets and closing ta are from the second part of the code. Sorry for the confusion, but I couldn’t see the edit optiob on my post.
[php]

<?php if(!empty($_SESSION["cart"])){ $total = 0; foreach ($_SESSION["cart"] as $key => $value) { ?>
                    <tr>
                        <td><?php echo $value["item_name"]; ?></td>
                        <td><?php echo $value["item_quantity"]; ?></td>
                        <td>$ <?php echo $value["product_price"]; ?></td>
                        <td>
                            $ <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td>
                        <td><a href="Cart.php?action=delete&id=<?php echo $value["product_id"]; ?>"><span
                                    class="text-danger">Remove Item</span></a></td>

                    </tr>
                    <?php
                    $total = $total + ($value["item_quantity"] * $value["product_price"]);
                }
                    ?>
                    <tr>
                        <td colspan="3" align="right">Total</td>
                        <th align="right">$ <?php echo number_format($total, 2); ?></th>
                        <td></td>
                    </tr>
                    <?php
                }
            ?>
        </table>
    </div>

</div>[/php]

hi mariade,

It is a good idea to turn on display errors before session_start:
[php]<?php
ini_set(‘display_errors’, 1);
session_start();[/php]

And change this
[php][/php]
to [php][/php]

Hope it’l help

thanks, I tried both advice but no luck, same errors. :frowning:

Can you make an archive with those files and attach to this topic?

Dump the contents of what is in tje session cart. I dont think you are deep enough, yet.

Dont use hidden prices. It is a very old way to do things and an easy way to get burned very quickly.

Hi Antonio,
I will post the zip files.
Thanks for your help.


reserve.php.zip (3.16 KB)

database file


tables.zip (3.02 KB)

images folder file contains only two images, the other are too big to attach, sorry.


cart.zip (223 KB)

Fixed :slight_smile:


reserve.zip (3.24 KB)

thanks Antonio, this morning I sorted too, only the table was out of place. thank you very much.
was it only a brackets problem?

I’ve found a bug: it won’t add more than one product to the cart, even if for the same product I would like quantity 2. If I add a different product, it will show me a window message saying that the product is already in the cart and it won’t add it.
How will i fix it?

Hi,

Programming is the art of dividing and hiding :slight_smile: So you should divide and hide. PHP-logic into one folder, HTML into another.

Here is a diagram of how to divide and realization.

This is reserve.php after dividing and hiding:
[php]<?php

chdir(DIR);

require_once ‘init.php’; // Hide common functions

$action = isset($_REQUEST[‘action’]) ? $_REQUEST[‘action’] : null;

switch ($action) {
case ‘add’:
include ‘cart/add.php’;
header(‘Location: reserve.php’);
break;

case 'delete':
    include 'cart/delete.php';
    header('Location: reserve.php');
    break;
    
// You have to write checkout.php
case 'checkout':
    include 'template/header.php';
    include 'template/checkout.php';
    include 'template/footer.php';
    break;
    
// You have to write cart/save.php and thank_you.php
case 'save_order':
    include 'cart/save.php';
    header('Location: thank_you.php');
    break;
    
default:
    include 'template/header.php';  // Split big HTML into parts: header, content and footer
    include 'template/products.php';
    include 'template/products_in_cart.php';
    include 'template/footer.php';
    break;

}[/php]


reserve-2.0.zip (230 KB)

I’m taking a look now, thanks for your time and clarification. I will keep you posted.

Ok, try this url or you can observe source code from the archive by this url

hope it helps :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service