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
https://forums.phpfreaks.com/topic/44886-solved-can-you-help-me-find-my-error/
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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.