Jump to content

Won't Add Item to the Cart


mrzebra81

Recommended Posts

Hi,

I'm hoping someone can lead me in the right direction here. I'm creating a simple shopping cart that I will later pass on to Paypal for payment but my problem is, it won't add the items to my shopping cart when I click add to cart.

 

My products are on different pages and I'm not sure if that is going to matter.

 

When I click "add to cart" nothing gets added and it says my cart is empty. I have two tables with the following columns:

 

1. products:

        productId

        productName

        productDesc

        productPrice

        link (for dynamically creating the page links on the main product page)

 

2. productscents

      scentId

      scentName

      scentDesc

 

 

Here is the add code that is in my first products page (Elite Hand Cream):

 

<?php 


if(isset($_GET['action']) && $_GET['action'] == "add") {
	$id = intval($_GET['id']);
	$desc=intval($_GET['desc']);

	if (isset($_SESSION['cart'][$id])) {
		$_SESSION['cart'][$id]['quantity']++; 
	}
	else {
		$sql3 = "SELECT * FROM products WHERE productId=[$id]";
		$sql4= "SELECT * FROM productscents WHERE scentId=[$desc]";
		$query3 = mysql_query($sql3);						
		$query4 = mysql_query($sql4);
		if(mysql_num_rows($query3) != 0) {
			$row3 = mysql_fetch_array($query3);
			$row4 = mysql_fetch_array($query4);
			$_SESSION['cart'][$row3['productId']] = array("quantity" => 1, "price" => $row3['productPrice'], "desc" => $row4['scentId']);
		} 
	}
}


?>

 

Here is code I have for the $_SESSION:

<?php 
	if(isset($_SESSION['cart'])) {
		$sql5= "SELECT * FROM products,productscents WHERE productId IN (";
		foreach($_SESSION['cart'] as $id => $value) {
			$sql5 .= $id . ",";				
		}
		$sql5 = substr($sql,0,-1) . ") ORDER BY productId, scentId ASC";
		$query = mysql_query($sql5);
		if(!empty($query)) {
			while($row = mysql_fetch_assoc($query)) {
	?>
	<?php echo $row['productName'];?><?php $_SESSION['cart'][$row['productId']]['quantity'];?>
	<?php 	
	}	 } else {
			echo "You need to add an item to your cart";}
	}	

 

 

Here is the code that I have for the "add to cart" and "view cart":

 

<a href=#&action=add&id=<?php echo $prodId;?>&desc=<?php echo $scent_id[$y];?>>Add to Cart</a><br><a href="cart.php">View Cart</a>	

 

 

This grabs the correct product Id and scent Id which I want and no errors are being displayed on the page.

 

I'm not sure where to begin with this. LOL. I'm new to PHP and I used a tutorial to help with the code.

 

Any help would be greatly appreciated.

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.