Jump to content

Query Executing twice in Shopping Cart


NickG21

Recommended Posts

Hey everyone my problem is as follows.  I have a custom shopping cart that i've been building for my store, and when I go to add a new product, I query the tblCart table to see if the PrID (ProductID) exists in the cart.  If it doesn't I insert the proper values with a qty of 1 and proceed. 

 

My problem is that when I insert the item initially with a value of 1, it acts like a page refresh and requeries the cart table, then having the ProductID in the cart, and updating it to a qty of 2 (as if a customer had clicked "Add to Cart" again on the product page after having one in the cart already).  My code is below, just for the "add_item" case in my switch statement.  Please let me know if you need anything more from me and thank you in advance for your help.

 

Lastly, this is from me starting over this morning on this switch case and still getting the same problem.

 

 switch($action) {    //decide what to do
         case "add_item":
         		/*check if product already in cart*/
         		$ExistSQL = "SELECT * from tblCart Where CartPrID ='" . $PrID . "'AND CartSessionID='" . $_SESSION['SessID'] . "'";
         			echo $ExistSQL . '<br/>';
         		$rsExistSQL = mysql_query($ExistSQL);
         		//if there are no results aka the product doesnt exist in this users cart then insert the product with a qty of 1
         		if(mysql_num_rows($rsExistSQL)==0){
         		echo "There are 0 results in this set<br/>";
         		$InsCartPr = "INSERT into tblCart (CartID, CartSessionID, CartPrID, CartPrOpt, CartPrQty, CartDateAdded) values ('','$_SESSION[sessID]','$PrID','$PrOpts','1',now())";
         			echo $InsCartPr . '<br/>';
         			$rsInsCartPr = mysql_query($InsCartPr);
         		}else{
         		//If there is a result, then the product is in the cart, update the cart qty by 1
         		$row = mysql_fetch_array($rsExistSQL);
         			$qty = $row['CartPrQty'] + 1;
         			echo $qty . '<br/>';
         		echo "Weve got something in the db, time to update<br/>";
         		$UpdCartPr = "Update tblCart set CartPrQty='" . $qty . "' WHERE CartSessionID = '" . $_SESSION['SessID'] . "' AND CartPrID='" . $PrID . "' AND  CartPrOpt = '" . $PrOpts . "'";
         			echo $UpdCartPr . '<br/>';
         			//$rsUpdCartPr = mysql_query($UpdCartPr);
         		}
break;

 

in the case of a switch statement like this, is it normal for it to iterate through the process again after a DB query has been executed?  im not a complete novice in PHP, but im no expert either.

Thanks again everyone,

Nick

Link to comment
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.