Cart array insert problem

hi

i m using this below code ref for creating shopping cart


 <?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

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.

Hi starlion

sorry for cutting

here are full queries 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

Well there’s your problem.

       foreach($_SESSION['cart_array'] as $key2 =&gt;$value2)

Why would you foreach that? You’re inserting -1- new item. Get rid of the foreach and insert $id and 1.

Thanks starlion

its working fine now

vineet