Shopping Cart - empty error

I have a site I maintain, mostly from a design standpoint. I know some PHP, enough to make some changes but am having an issue that I cannot figure out and hoping someone here can help me.

From what I know, this seems to be happening only in Firefox.

They add an item(s) to the cart > click Proceed to Checkout > it says “You have no items in your cart.”

<snip />

Thank you for any assistance or guidance you can give me to resolve this.

Post your code, otherwise, there isn’t much we can do. Please remove any confidential information (usernames, passwords, etc).

Suppose that would help. Here it is:

<?

function check_if_empty($orderid, $orderitems_table)
{
	$query = "SELECT * FROM {$orderitems_table} WHERE item_orderid = {$orderid}";
	$items = doSelect($query);
	if(count($items) < 1)
	{
		setcookie("orderid", 0, time()-(3600*48), '/');
	}
}

$price_to_use = "prod_price";
$checkout = "checkout";
$order_table = "orders";
$orderitems_table = "orderitems";

foreach($_POST as $name => $value){ $$name = $value; }




if ($mode == "add"){
	
	if(!$orderid)
	{
		$query = "insert into orders (order_id, order_startdate) values ('', date_sub(now(), interval 2 hour))";
		$i = doInsert($query);
		$orderid = mysql_insert_id();
		setcookie("orderid", $orderid, time()+(3600*12), '/');
	}
	
	$qty = 0;
	if (isset($_POST['qty'])) { $qty = numberfy($_POST['qty']); }

	if ($qty < 1){ $qty = 1; }

	$query = "select * from products where prod_id = $prodid";
	$results = doSelect($query);
	foreach($results[0] as $name => $value){
		$$name = $value;
	}
	$prod_name = addslashes($prod_name);
	$actual_price = $$price_to_use;

	$count = 1;
	$name = "optionset_$count";
	while ($_POST[$name]){
		$value = htmlspecialchars($_POST[$name]);
		$prod_name .= ", $value";
		$count++;
		$name = "optionset_$count";
	}
		
	$oldqty = 0;
	// Check to see if item (with same attributes) is already in cart
	$query = "select item_qty from $orderitems_table where item_prodid = $prod_id and item_name = '$prod_name' and item_orderid = $orderid";
	$results = doSelect($query);
	$oldqty = $results[0][0];

	echo "oldqty = " . $oldqty . '<br />';
	if ($oldqty){  // Add qty to existing item
		$newqty = $qty + $oldqty;
		$query = "update $orderitems_table set item_qty = $newqty where item_prodid = $prod_id and item_name = '$prod_name' and item_orderid = $orderid";
		$u = doUpdate($query);
		echo "update";
	} else {  // Add new item
		$query = "insert into $orderitems_table (item_id, item_prodid, item_orderid, item_name, item_sku, item_priceeach, item_qty) ";
		$query .= "values ('', $prod_id, $orderid, '$prod_name', '$prod_sku', $actual_price, $qty)";
		$i = doInsert($query);
		echo "insert";
	}
	header("Location: /cart/");
	exit();
}

if ($mode == "update"){
	$query = "select item_id, item_qty, item_wrap from $orderitems_table where item_orderid = $orderid";
	$items = doSelect($query);
	foreach($items as $item){
		$newqty = numberfy($_POST["item_qty_$item[0]"]);
		if ($newqty > 0 && $newqty < 1000 && $newqty != $item[1]){
			$query = "update $orderitems_table set item_qty = $newqty where item_id = $item[0]";
			$u = doUpdate($query);
		}
		$newwrap = numberfy($_POST["item_wrap_$item[0]"]);
		if (($newwrap == 0 || $newwrap == 1) && $newwrap != $item[2]){
			$query = "update $orderitems_table set item_wrap = $newwrap where item_id = $item[0]";
			$u = doUpdate($query);
		}
	}
	check_if_empty($orderid, $orderitems_table);
	header("Location: /cart/");
}

if ($mode == "remove"){
	$itemid = numberfy($_GET['var2']);
	$query = "delete from $orderitems_table where item_id = $itemid and item_orderid = $orderid";
	$d = doDelete($query);
	check_if_empty($orderid, $orderitems_table);
	header("Location: /cart/");
}

if ($orderid){
	$query = "select * from $orderitems_table where item_orderid = $orderid";
	$items = doSelect($query);
}

$pagetitle = "Cart";
include("template_main-top.php");
include('mainmenu.php');
?>

<div id="topbar">&nbsp;</div>

<img src="/images/cart_header.gif">

<div id="cart">
	
	<form action="updatecart/" method="post" id="cartform">
	<input type="hidden" name="func" value="cart" />
	<input type="hidden" name="mode" value="update" />
	<input type="hidden" name="catid" value="<? echo $catid; ?>" />
	<input type="hidden" name="page" value="<? echo $page; ?>" />
	<input type="hidden" name="subcatid" value="<? echo $subcatid ?>" />
	
	<? if($items): ?>
		<div id="top_buttons">
			<a href="/shop" class="shop"><img src="/images/button_keepshopping.gif"></a>
			<a href="#" onClick="document.forms['cartform'].submit(); return false;" class="update"><img src="/images/button_updatecart.gif"></a><br/>
		</div>
	<? endif; ?>
	
	<table id="cart">
		<tr id="cartbar">
			<td class="desc" colspan="2"><strong>ITEM DESCRIPTION</strong></td>
			<td class="price"><strong>PRICE</strong></td>
			<td class="qty"><strong>QTY</strong></td>
			<td class="subtot"><strong>SUBTOTAL</strong></td>
			<td class="remove">&nbsp;</td>
		</tr>
		<?
		$subtotal2 = 0;
		if($items){
			foreach ($items as $item){
				foreach($item as $name => $value){ $$name = $value; }
				$subtotal = ($item_qty * $item_priceeach);
				$subtotal2 = $subtotal2 + $subtotal;
				
				// Get main picnum
				$query = "select prodpics_picnum from prodpics where prodpics_prodsku = '$item_sku' order by prodpics_order limit 1";
				$results = doSelect($query);
				$mainpic = $results[0][0];
				?>
				<tr>
					<td><img src="/prod_images/<? echo $item_sku; ?>__<? echo $mainpic; ?>-tiny.jpg" /></td>
					<td><?= $item_name ?></td>
					<td>$<?= $item_priceeach ?></td>
					<td><input type="text" name="item_qty_<?= $item_id ?>" value="<?= $item_qty ?>" size="2" maxlength="3" /></td>
					<td><? printf("\\$%.2f", $subtotal); ?></td>
					<td><a href="/cart/remove/<?=$item_id?>/">remove</a></td>
				</tr>
				<?
			}
		} else {
			?>
			<tr>
				<td colspan="6">You have no items in your cart.</td>
			</tr>
			<?
		}
		$total = $subtotal2;
		?>
	</table>
	
	<? if($items): ?>
		<div id="subtotal"><strong>Subtotal:</strong> <em>$<? printf("%.2f", $subtotal2); ?></em></div>
		<div id="proceed_button">
			<a href="https://<?= $root ?>/checkout/" class="checkout"><img src="/images/button_proceedtocheckout.gif"></a>
		</div>
	<? endif; ?>
	
	</form>

</div>

<?
include("template_main-bottom.php");
?>

hi cpradio - did you add to my code above? Trying to see if it’s different, if so, where.

Thx.

If this helps at all, yesterday I added one item and when I clicked to checkout it said no items in the cart. If I click back on the “Shop” link, which takes me back to the main shopping page, it shows 0 items in the cart, but if I click “continue shopping” it will still show 1 item in the shopping cart.

If I then add a second item, then click “Proceed to checkout” it gives me the form to complete with name, address, etc.

Could be that you’re starting at www.example.com and your checkout link goes to example.com (no www). This would put you in a different session. I’ve seen this before. If so, the solution would be to force all traffic to either with or without www (which may be a good idea regardless).

I don’t see in the code where it is specifying the domain, just the code for the cart, items, etc.

Your Proceed button has
href=“https://<?= $root ?>/checkout/”

but nothing else uses that $root variable. I can’t be sure that’s the issue, but check it out.

hmm how interesting!

OK, it works for me in Firefox on the Mac but NOT in Firefox on PC (Win7) .

As QMonkey points out - you are going from http to https and no www. to https://www. so your cookie may not be respecting the domain change.
Var_dump the cookie contents on the cart page and see if they are there.

I would point to the cookie as being the issue and how its being read my FF on PC.

Thanks QMonkey and spikeZ.

My homework now - to figure out how to do this. A bit beyond my knowledge at this point, but every day is a learning day. I’ll let you know how it goes.

This code is in my globals.php - does this help at all?

<?

$root = “www.shoplilygirl.com”;