Hello I have created an add to cart function but when I click on add to cart it replace the previously added products

Here is my code

Header Area details.php
<?php
    require_once("includes/functions.php");

    if(isset($_POST["addtocart"])) {
        $pid        = $_POST["pid"];
        $prod_name  = $_POST["productname"];
        $prod_price = $_POST["productprice"];
        $category   = $_POST["cat_name"];

        if(isset($_POST['command']) == 'add' && $_POST['pid']>0 ) {
            echo addtocart($pid,1, $prod_name, $prod_price, $category);
        }
    }

    require_once("includes/header.php");

    if (isset($_GET['pid'])) {
        $pid = $_GET['pid'];
    }

    if(isset($_GET["cat"])) {
        $category_name = mysqli_real_escape_string($connection, $_GET["cat"]);
    }
?>
Content Area details.php

<div id="products-details"> <div class="row"> <?php
                    $result = mysqli_query($connection, "select * from products where prod_id='$pid'");
                    while ($row = mysqli_fetch_array($result)) {
                        $prod_name    = $row['product_name'];
                        $prod_details = $row['description'];
                        $prod_price   = $row['product_price'];
                        $prod_img     = $row['product_image'];
                        $prod_id      = $row['prod_id'];
                    }
                ?> <form method="POST" action="details.php"> <div class="col-md-4"> <img src="admin/uploads/<?php echo $prod_img; ?>" /> </div> <div class="col-md-8" style="background:#fff;"> <div class="heading1"> <h3><i class="fa fa-reorder"></i> <?php echo $category_name; ?> </h3> </div> <div class="product-descrip"> <h6> <?php echo $prod_name; ?> </h6> <p class="price">
                                $<?php echo $prod_price; ?> </p> <p class="description"> <?php
                                    if(isset($prod_details)) {
                                        echo $prod_details;
                                    }
                                ?> </p> <input type="hidden" name="productname" value="<?php echo $prod_name; ?>"/> <input type="hidden" name="productprice" value="<?php echo $prod_price; ?>"/> <input type="hidden" name="productid" value="<?php echo $prod_id?>"/> <input type="hidden" name="cat_name" value="<?php echo $category_name; ?>" /> <input type="hidden" name="command" value="add" /> <input type="hidden" name="pid" value="<?php echo $prod_id?>" /> <div class="btnadd"> <input type="submit" class="btn btn-default btnred" value="Add to Cart" name="addtocart" /> <a href="products.php?cat=<?php echo $category_name; ?>" class="btn btn-default btnred">Back</a> </div> </form> </div> </div>

and here is my addtocart function

function addtocart($pid,$q,$pname,$pprice){
        if($pid<1 or $q<1) return;

        if(is_array($_SESSION['cart'])){
            if(product_exists($pid)) return;
            $max=count($_SESSION['cart']);
            $_SESSION['cart'][$max]['productid']=$pid;
            $_SESSION['cart'][$max]['qty']=$q;
        }
        else{
            $_SESSION['cart']=array();
            $_SESSION['cart'][0]['productid']=$pid;
            $_SESSION['cart'][0]['qty']=$q;
        }

        header("location:shoppingcart.php?pname=$pname&price=$pprice");
    }
    function product_exists($pid){
        $pid=intval($pid);
        $max=count($_SESSION['cart']);
        $flag=0;
        for($i=0;$i<$max;$i++){
            if($pid==$_SESSION['cart'][$i]['productid']){
                $flag=1;
                break;
            }
        }
        return $flag;
    }

Recommended Answers

All 5 Replies

At line number 12
echo addtocart($pid,1, $prod_name, $prod_price, $category); 5 parameters
But this function
function addtocart($pid,$q,$pname,$pprice){ 4 parameters

changed and still having same issue I noticed these 3 bugs

1 If the cart is empty and I add a product having id 1 . Added perfectly and when I try to add second product hacing id 3 2 or 4 it adds and replace the previous one now cart will be like this
before
1 product 1 $304
after adding second product cart will be like this
1 product 2 $673
2 product 3 $673

Got my point so I sontknow why is this happening

Member Avatar for diafol

changed and still having same issue I noticed these 3 bugs

You should show updated code (relevant bits only). Your addtocart function is pretty suspect. Looks overly complicated.

An alternative to checking the cart array every time would be to set cart array item indexes to pid.

function addtocart($pid,$q,$pname,$pprice){
    if(!filter_var($pid) && !filter_var($q)) return;
    if($pid <= 0 || $q <= 0) return;

    if(!isset($_SESSION['cart'])) $_SESSION['cart'] = []; 
    if(!isset($_SESSION['cart'][$pid]))
    {
        $_SESSION['cart'][$pid]['qty']=$q;
        //store other details??
    }else{
        return; // must be better way than this?!
    }
    header("location:shoppingcart.php?pname=$pname&price=$pprice");
}

Hello,

This one is the updated code and the function you sent me is little bit confusing me and is not wworking as well here is the error i am getting

Notice: Undefined index: productid in C:\wamp\www\homemart\shoppingcart.php on line 63

line 61 to line 67

<?php
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
    $pid=$_SESSION['cart'][$i]['productid'];
    $q=$_SESSION['cart'][$i]['qty'];
    $pname=$_GET['pname'];;
    if($q==0) continue;
?>

function

function addtocart($pid, $q, $pname, $pprice, $category){
        if($pid<1 or $q<1) return;

        if(is_array($_SESSION['cart'])){
            if(product_exists($pid)) return;
            $max=count($_SESSION['cart']);
            $_SESSION['cart'][$max]['productid']=$pid;
            $_SESSION['cart'][$max]['qty']=$q;
        }
        else{
            $_SESSION['cart']=array();
            $_SESSION['cart'][0]['productid']=$pid;
            $_SESSION['cart'][0]['qty']=$q;
        }

        header("location:shoppingcart.php?pname=$pname&price=$pprice&cat=$category");
    }

Your $_SESSION['card'] is created as an array, but then you are using each element as an associated array...

Edited: I am guessing you are missing $_SESSION['cart'][0]=array();. You attempt to assign an associative array to an element of an array...

PS: I still don't feel comfortable with the way you structure your session value though (too complex)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.