Jump to content

PHP Shopping Cart


lewisstevens1

Recommended Posts

Hiya guys, I have this script i have wrote, it seems to work although there's a little problem.

 

<?php
include 'login/dbc.php';
page_protect();

if (isset($_SESSION['user_id'])) {
$user = $_SESSION['user_id'];
$quantity = $_POST["postquantity"];
$prod_id = $_POST["postname"];

$rs_duplicate = mysql_query("select count(*) as total from cart where product_id='$prod_id' AND cart_user=".$_SESSION['user_id']."") or die(mysql_error());
$result = mysql_query("SELECT * FROM cart WHERE product_id='$prod_id' AND cart_user=".$_SESSION['user_id']."");

list($total) = mysql_fetch_row($rs_duplicate);
if ($total > 0)
{
while($row = mysql_fetch_array($result))
{
	$tot_quantity = $quantity + $row['quantity'];
	mysql_query("UPDATE cart SET
		quantity='$tot_quantity' WHERE cart_user=".$_SESSION['user_id']." AND product_id='$prod_id'");
	echo "#". $tot_quantity;
}
} else {
mysql_query("INSERT into cart
(
	cart_user,
	product_id,
	quantity
)
VALUES
(
	'$user',
	'$prod_id',
	'$quantity'
)
");
}


echo "Product ID: $prod_id";
echo "<br>quantity_calc:  ".$quantity_calc." <br>";
echo "Quantity: $quantity";
echo "<br><br>";

echo "
	i need to select the productid,
	we will then use this to insert into cart.
	also insert our userid into cart user
";
}else{ echo "no stop!"; }?>

 

This is just ment to check if the product allready exists and then update depending on the value thats sent to it...

Now it does work, theres just one problem though.. It randomly when changing the values on the two products i have in the database, it decides to update both... Although most of the time it works.. Why is it that it randomly does it??

 

Many thanks

Lewis Stevens

Link to comment
https://forums.phpfreaks.com/topic/264547-php-shopping-cart/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.