SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Cart array insert problem

    hi

    i m using this below code ref for creating shopping cart

    PHP Code:
     <?php 
    if (isset($_POST['pid'])) {
        
    $pid $_POST['pid'];
        
    $wasFound false;

        if (!isset(
    $_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
            
    $_SESSION["cart_array"] = array(array("item_id" => $pid"quantity" => 1));

            
    /* query to insert items into cart */
            
    $qry "INSERT into cart_table() values()";
            
        } else {
            foreach (
    $_SESSION["cart_array"] as &$each_item) { 
                if (
    $each_item['item_id'] == $pid) {
                    
    $each_item['quantity'] += 1;
                    
            
    /* query to update items quantity in cart */
            
    $cart_update_query "UPDATE cart_table SET ....."

            
    $wasFound true;
                    break;
                }
            }

            if (
    $wasFound == false) {
                
    array_push($_SESSION["cart_array"], array("item_id" => $pid"quantity" => 1));

                
    /* query to insert items into cart */
                
    $qry "INSERT into cart_table() values()";
            }
        }
        
    header("location: cart.php"); 
        exit();

    ?>
    The problem :
    If i insert ONE product then all is fine.

    WHEN i insert SECOND different product then 2nd product is added fine But 1st product is also added again into new row.

    Means i get 3rows (2 rows of 1st product and 1 row of 2nd product in database)

    i should get 2rows

    If i remove the INSERT QUERY from after array_push code then 2nd different product doesnt get added.

    vineet

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    You need to not cut out the queries when pasting the code to us, or we cant help you solve the problem with the queries.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi starlion

    sorry for cutting

    here are full queries code

    PHP Code:
    <?
    $unique_id 
    session_id(); 
    if (!isset(
    $_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
            
    $_SESSION["cart_array"] = array(array(
            
    "product_id" => $id"quantity" => 1));
                                            
        foreach(
    $_SESSION['cart_array'] as $key =>$value)
        {
        
    $cart_query "INSERT INTO cart_table(unique_id,product_id,quantity) VALUES('$unique_id',$value[product_id],$value[quantity])";
        
    $cart_result=mysql_query($cart_query)or die(mysql_error());
        }
                                        
        } else {
            foreach (
    $_SESSION["cart_array"] as &$each_item) { 
                     if (
    $each_item['product_id'] == $id) {
                    
    $each_item['quantity'] += 1;
            
    $cart_update_query "UPDATE cart_table SET product_id=$each_item[product_id], quantity=$each_item[quantity] where product_id=$id and unique_id='$unique_id'"
            
    $cart_update_result=mysql_query($cart_update_query)or die(mysql_error());
            
    $wasFound true;
                    break;
                }
            }

            if (
    $wasFound == false) {
                
    array_push($_SESSION["cart_array"], array(
                
    "product_id" => $id"quantity" => 1));
                                            
                foreach(
    $_SESSION['cart_array'] as $key2 =>$value2)
                {
                
    $cart_query2 "INSERT INTO cart_table(unique_id,product_id,quantity) 
                    VALUES('
    $unique_id',$value2[product_id],$value2[quantity])";
                
    $cart_result2=mysql_query($cart_query2)or die(mysql_error());
                }
    }                        
    }
    }    
    ?>
    vineet

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Well there's your problem.

    foreach($_SESSION['cart_array'] as $key2 =>$value2)

    Why would you foreach that? You're inserting -1- new item. Get rid of the foreach and insert $id and 1.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks starlion

    its working fine now

    vineet


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •