The first time I add a item to my basket I use this code:
Code: Select all
$product_id = $_POST['productid'];
// db connect
require("db_inc.php");
if (!$db_selected)
{
** or die ("error : " . mysql_error());
}
$result = mysql_query("SELECT product_id, image_small, artist, short_desc, price FROM products WHERE product_id LIKE '$product_id'");
if (!$result)
{
** or die ('Invalid query: ' . mysql_error());
}
// search for new item in basket
$itemfound = 0;
$position = 0;
$saveposition = 0;
foreach ($_SESSION['basket'] as $item)
{
if ($item["product_id"] == $product_id)
{
$itemfound = 1;
$saveposition = $position;
}
$position = $position + 1;
}
$item = array();
if ($itemfound == 1)
{
// item exists, update quantity
while($row=mysql_fetch_array($result))
{
$item = $_SESSION['basket'][$saveposition];
$item['quantity'] = $item['quantity'] + $_POST['text'];
// add item to end of shopping basket
$_SESSION['basket'][$saveposition] = $item;
}
}
else
{
// else just add item to basket
while($row=mysql_fetch_array($result))
{
$item['product_id'] = $row["product_id"];
$item['image_small'] = $row["image_small"];
$item['artist'] = $row["artist"];
$item['short_desc'] = $row["short_desc"];
$item['price'] = $row["price"];
$item['quantity'] = $_POST['text'];
// add item to end of shopping basket
$_SESSION['basket'][] = $item;
}
}When I add another item to the basket the first one dissapears. But if I update the quantity of the first item like this everything works from there but, the first item in the basket is empty. It have no price, picture, description and so on:
Code: Select all
// get product_id and quantity
$product_id = $_POST['value'];
$quantity = $_POST['text'];
// search for product_id in basket
$position = 0;
$saveposition = 0;
foreach ($_SESSION['basket'] as $item)
{
if ($item["product_id"] == $product_id)
{
$saveposition = $position;
}
$position = $position + 1;
}
// update quantity
$item = array();
$item = $_SESSION['basket'][$saveposition];
$item['quantity'] = $quantity;
$_SESSION['basket'][$saveposition] = $item;Can someone please tell me what's wrong?