SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update Shopping cart problem

    Hi All

    Could somebody please have a look at this code for me. I have been tearing my hair out all day today and am getting nowhere fast.

    Code:
    function updateCart()
    {
    	$cartId     = $_POST['hidCartId'];
    	$productId  = $_POST['hidProductId'];
    	$itemQty    = $_POST['txtQty'];
    	$numItem    = count($itemQty);
    	$numDeleted = 0;
    	$notice     = '';
    	
    	for ($i = 0; $i < $numItem; $i++) {
    		$newQty = (int)$itemQty[$i];
    		if ($newQty < 1) {
    			// remove this item from shopping cart
    			deleteFromCart($cartId[$i]);	
    			$numDeleted += 1;
    		} else {
    			// check current stock
    			$sql = "SELECT pd_name, pd_qty
    			        FROM tbl_product 
    					WHERE pd_id = {$_POST[hidProductId][$i]}";
    			$result = dbQuery($sql);
    			$row    = dbFetchAssoc($result);
    			
    			if ($newQty > $row['pd_qty']) {
    				// we only have this much in stock
    				$newQty = $row['pd_qty'];
    
    				// if the customer put more than
    				// we have in stock, give a notice
    				if ($row['pd_qty'] > 0) {
    					setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the &quot;Quantity&quot; box. ');
    				} else {
    					// the product is no longer in stock
    					setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock');
    
    					// remove this item from shopping cart
    					deleteFromCart($cartId[$i]);	
    					$numDeleted += 1;					
    				}
    			} 
    							
    			// update product quantity
    			$sql = "UPDATE tbl_cart
    					SET ct_qty = $newQty
    					WHERE ct_id = {$cartId[$i]}";
    				
    			dbQuery($sql);
    		}
    	}
    	
    	if ($numDeleted == $numItem) {
    		// if all item deleted return to the last page that
    		// the customer visited before going to shopping cart
    		header("Location: $returnUrl" . $_SESSION['shop_return_url']);
    	} else {
    		header('Location: cart.php');	
    	}
    	
    	exit;
    }
    When I run it I am constantly getting "Unknown column 'A' in 'where clause'..."

    Many thanks. Please be gentle with me! I am not an expert programmer

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First, figure out which of the two queries the error is happening at. Add some nonsense output so you can figure out which line results in the error being printed.

    Then figure out exactly what the query is. Print $sql.

    By then you should have a pretty good idea what's wrong.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm well CartID should be a number! When an item is added to the shopping cart, the mysql database gives it an automatic number and the column is ct_id.

    I have the following on my input form for adding items to the cart.

    Code:
                    <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>">
                    <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>"></td>
    which obviously works ok when adding items to the cart and I can update the cart quantities fine if I just keep pressing the add to cart button!! However, once I have gone on to the next page ie, the view shopping cart page and try and do an update from there it all goes pear shaped! Thats when I get the Unknown column error. The delete cart bit works ok

    Code:
    /*
    	Remove an item from the cart
    */
    function deleteFromCart($cartId = 0)
    {
    	if (!$cartId && isset($_GET['cid']) && (int)$_GET['cid'] > 0) {
    		$cartId = (int)$_GET['cid'];
    	}
    
    	if ($cartId) {	
    		$sql  = "DELETE FROM tbl_cart
    				 WHERE ct_id = $cartId";
    
    		$result = dbQuery($sql);
    	}
    	
    	header('Location: cart.php');	
    }
    Many thanks

    Lizzy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •