Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.