Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I need some help with my basket its adding the item, but on the basket.php shopping cart is empty.

What I have tried:

              <div id="basket-overview" class="navbar-collapse collapse d-none d-lg-block">
  <a href="basket.php" class="btn btn-primary navbar-btn">
    class="fa fa-shopping-cart">
    <?php echo isset($_SESSION['cart']) ? '<span id="cart-count">' . count($_SESSION['cart']) . '</span>' : ''; ?>

<div id="basket" class="col-lg-9">
              <div class="box">
                <form method="post" action="add_to_cart.php">
                  <h1>Shopping cart</h1>
                  <p class="text-muted">You currently have <span id="cart-count"><?php echo isset($_SESSION['cart']) ? '<span id="cart-count">' . count($_SESSION['cart']) . '</span>' : ''; ?>

                  <p id="cart-message" class="text-success" style="display: none;"></p>
                  <div class="table-responsive">
                    <table class="table">
                          <th colspan="2">Product</th>
                          <th>Unit price</th>
                          <th colspan="2"><span id="total-amount">Total</span></th>

                      <tbody id="cart-items">
                          <td><a href="#"><img src="img/detailsquare.jpg" alt="White Blouse Armani"></a></td>
                          <td><a href="#">White Blouse Armani</a></td>
                <div class="input-group">
                <button class="btn btn-default btn-minus" type="button">-</button>
                <input type="number" name="product1" value="0" min="1" class="form-control product-quantity" data-product-id="1">
              <button class="btn btn-default btn-plus" type="button">+</button>

// jquery code
function addToCart(productId, quantity) {
  var cart = getCart();
  if (cart.hasOwnProperty(productId)) {
    var newQuantity = parseInt(quantity);
    if (newQuantity <= 0) {
      delete cart[productId];
    } else {
      cart[productId] = newQuantity;
  } else {
    cart[productId] = parseInt(quantity);

  // Display message
  var productName = $('#product-name-' + productId).text();
  $('#cart-message').text(quantity + 'x ' + productName + ' added to cart').show();
  setTimeout(function() {
  }, 3000);

function calculate_total() {
  var cart = getCart();
  var total = 0;
  for (var productId in cart) {
    var product = getProductId(productId);
    var price = parseFloat(product.price);
    var quantity = parseInt(cart[productId]);

    // Send request to server to get discount for the product
      url: 'get_product_details.php',
      data: {productId: productId},
      dataType: 'json',
      success: function(data) {
        // Update HTML elements with discount and amount
        var discount = parseFloat(;
        var amount = price * quantity * (1 - discount);
        $('#discount-' + productId).text((discount * 100).toFixed(0) + '%');
        $('#amount-' + productId).text('$' + amount.toFixed(0));
      error: function(xhr, textStatus, errorThrown) {
        console.log('Error getting product details: ' + errorThrown);

// Start the session

echo count($_SESSION['cart']);
// Check if the cart is not empty
if (!empty($_SESSION['cart'])) {
    // Establish database connection.
    $conn = mysqli_connect('localhost', 'root', '', 'ecommerce_store');
    if (!$conn) {
        die('Could not connect to database: ' . mysqli_connect_error());

    // Query the database to get the products in the cart
    $cart_items = array();
    foreach ($_SESSION['cart'] as $product_id => $quantity) {
        $sql = "SELECT * FROM products WHERE productID = '$product_id'";
        $result = mysqli_query($conn, $sql);
        if (!$result) {
            die('Error getting product details: ' . mysqli_error($conn));
        $product = mysqli_fetch_assoc($result);
        $product['quantity'] = $quantity;
        $cart_items[] = $product;

    // Close the database connection
<!DOCTYPE html>
    <title>Shopping Cart</title>
    <link href="" rel="stylesheet">
<div class="container">
    <h1>Shopping Cart</h1>
    <?php if (!empty($cart_items)): ?>
        <table class="table">
                    <th>Product Name</th>
                    <th>Unit Price</th>
                <?php foreach ($cart_items as $cart_item): ?>
                    <td><img src="<?php echo $cart_item['image']; ?>" alt="<?php echo $cart_item['product_name']; ?>"></td>
                    <td><?php echo $cart_item['product_name']; ?></td>
                    <td>$<?php echo $cart_item['unit_price']; ?></td>
                    <td><?php echo $cart_item['quantity']; ?></td>
                    <td><?php echo $cart_item['discount']; ?></td>
                    <td>$<?php echo ($cart_item['unit_price'] * $cart_item['quantity'] * (1 - $cart_item['discount'])); ?></td>
                <?php endforeach; ?>
    <?php else: ?>
        <p>Your cart is empty.</p>
    <?php endif; ?>
Member 15627495 25-Apr-23 1:52am    
to debug you have to check your vars, all along your process and functions.

to achieve : by JS
console.log(the_var) ;
console.table(the_array_var) ;

by Php :
var_dump($the_var_all_types) ;
print_r($var_arrays_object) ;

those functions will provide you inner values for the vars read.

as one more comment, handling cart and basket from client side is risky... because datas are too 'exposed for user'
really migrate this feature to server side ( php + database table )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900