I have created a shopping cart with sessions and just found a problem
When a user checkout it added all the data along with the session ID into the DB.
IF the user then goes back and orders something else BEFORE closing the window it will give a new order number, but add the product data into the other order with the Session ID
This screws everything up
How do ig et round this?
if (is_array($_SESSION['products']))
{
$i=1;
foreach($_SESSION['products'] as $key=>$val)
{
$stock_update="UPDATE products SET stock=stock-1 WHERE id='".$val."'";
mysql_query($stock_update) or die (mysql_error());
if ($i==2)
{
$query2=mysql_query("SELECT * FROM `products` WHERE id='".$val."'");
if (mysql_num_rows($query2)==1)
{
$prodinfo=mysql_fetch_assoc($query2);
$checkitem=mysql_query("SELECT * FROM `shopping_cart_order_items` WHERE sessionid='".session_id()."' AND productid='".$val."'");
if (mysql_num_rows($checkitem)==1)
{
/////////////////////////////////////THIS LINE IS THE UPDATE ONE. ///////////////////////////////
$updatequery=mysql_query("UPDATE shopping_cart_order_items SET quantity=quantity+1 WHERE sessionid='".session_id()."' AND productid='".$val."'");
echo "<br>".$val." --- ".$prodinfo['productname']." --- Price: £".$prodinfo['price']." --- <a href=\"infobox.php?id=$val\" onClick=\"return popitup('infobox.php?id=$val')\"><img src=\"i_help.gif\" width=\"16\" height=\"16\" border=\"0\"></a>";
}
else
{
$count=$count++;
$query3=("INSERT INTO shopping_cart_order_items (productname,orderid,sessionid,productid,price,quantity) VALUES ('".$prodinfo['productname']."','".$orderid."','".session_id()."','".$val."','".$prodinfo['price']."','1')");
mysql_query($query3) or die (mysql_error());
echo "<br>".$val." --- ".$prodinfo['productname']." --- Price: £".$prodinfo['price']." --- <a href=\"infobox.php?id=$val\" onClick=\"return popitup('infobox.php?id=$val')\"><img src=\"i_help.gif\" width=\"16\" height=\"16\" border=\"0\"></a>";
}
@$carttotal=@$carttotal+$prodinfo['price'];
$updatecart=mysql_query("UPDATE `orders` SET numitems=numitems+1 WHERE id='".$orderid."'");
}
$i=1;
} $c = array($carttotal+20.00);
$totalvalue = "".array_sum($c)."";
$i++;
}
$updatecart=mysql_query("UPDATE `orders` SET total='$carttotal' WHERE id='$orderid'");
}
David