Jump to content

[SOLVED] Can you help me find my error?


spamoom

Recommended Posts

I'm making a simple shopping cart and I have had the "add to cart" part working, but for some strange reason it wont work anymore. Here is the code

 

<?php
case "add":
$itemid = $_GET['id'];
$itemid = $core->cleanvar($itemid);
$sql = "SELECT COUNT(itemid) FROM cart_items WHERE itemid='{$itemid}' AND cartid='{$cartid}'";
$query = $db->query($sql);
$count = $db->count($query);
if ($count != 0) {
	$sql = "SELECT * FROM cart_items WHERE itemid='{$itemid}' LIMIT 0,1";
                echo "DEBUG: gonna update the row";
	$query = $db->query($sql);
	$data = $db->fetch($query);
	$quantity = $data['quantity'] + 1;
	$sql = "UPDATE cart_items SET quantity='{$quantity}' WHERE itemid='{$itemid}'";
	$query = $db->query($sql);
	if (!$query) {
		echo "Cannot add to cart!";
	} else {
		//header ('Location: index.php?s=cart');
	}
} else {
	$sql = "INSERT INTO cart_items (id, cartid, itemid, quantity) VALUES (0, {$cartid}, {$itemid}, 1)";
	echo "DEBUG: inserting a new row";
	$query = $db->query($sql);
	if (!$query) {
		echo "Cannot add to cart!";
	} else {
		//header ('Location: index.php?s=cart');
	}
}
        //debug
echo "itemid: ".$itemid."<br/>";
echo "cartid: ".$cartid."<br/>";
break;
?>

 

When I add an item that already exists, the quantity updates like it should which is $count !=  0. Problem is... if it does equal 0 it doesnt do anything in my else not even echo "DEBUG: inserting a new row";.

 

Please help me solve my mistake or typo =P

 

Thanks  ;D

 

SpaM!

Link to comment
Share on other sites

I dont know what youre talking about chronister, but != is the reverse of == and !== is something else, although I does in most things the same thing, you can get troubles with it later...check out php.net for more info.

 

I can't see any problem in your script, you sure $count equals 0? Maybe echo it and see if it really is 0. And I would like to advice you the use or brackets for every case...its like not using brackets with if's.

 

Full-Demon

Link to comment
Share on other sites

Ok, thats wierd... solved my own problem

 

changed it from

 

$sql = "SELECT COUNT(itemid) FROM cart_items WHERE itemid='{$itemid}' AND cartid='{$cartid}'";
to
$sql = "SELECT itemid)FROM cart_items WHERE itemid='{$itemid}' AND cartid='{$cartid}'";

 

And it works =P

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.