spamoom Posted March 30, 2007 Share Posted March 30, 2007 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 SpaM! Quote Link to comment Share on other sites More sharing options...
chronister Posted March 30, 2007 Share Posted March 30, 2007 $count !== 0) { Try that, the single = sets a var, the == evaluates the expresion. Quote Link to comment Share on other sites More sharing options...
spamoom Posted March 30, 2007 Author Share Posted March 30, 2007 Nope, still displays DEBUG: gonna update the row Even though it equals 0... =( Quote Link to comment Share on other sites More sharing options...
spamoom Posted March 30, 2007 Author Share Posted March 30, 2007 bump Quote Link to comment Share on other sites More sharing options...
nikon Posted March 30, 2007 Share Posted March 30, 2007 add this code: echo $count; After: echo "DEBUG: gonna update the row"; Quote Link to comment Share on other sites More sharing options...
Full-Demon Posted March 30, 2007 Share Posted March 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
spamoom Posted March 30, 2007 Author Share Posted March 30, 2007 Ok, I've had a look at count and for some reason is equaling 1 even though when i run the sql query in phpmyadmin equals 0 :S. So it is trying to update the query when it ain't there =( Quote Link to comment Share on other sites More sharing options...
spamoom Posted March 30, 2007 Author Share Posted March 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.