Jump to content
Larry Ullman's Book Forums

Chapter 9: Can't Get Cart.html To Display It Keeps Showing Emptycarts.html


Recommended Posts

Hi,

I've been troubleshooting the Creating the Views section in Chapter 9 and I can't seem to figure out why it keeps displaying the emptycart.html after I click add to Cart for the Kona Decaf item?  It checks if the rows returned back from the Stored Proc is greater than zero.  It should be as I added the item but yet it keeps showing the HTML for emptycart.html and not the cart.html file.

To troubleshoot to see if the "if else" and the cart.html is working I changed the code to if the rows returned are <=0 and it finally showed my cart.html.  But why doesn't it work when it is  $r > 0? 

 

if (mysqli_num_rows($r) > 0) 
{ // Products to show!
include ('/views/cart.html');
} else
{ // Empty cart!
include ('/views/emptycart.html');
}


 

 

Link to comment
Share on other sites

Do an assignment to a $row_count variable Instead of evaluating mysqli_num_rows($r) inside an 'if'.

 

if (mysqli_num_rows($r) > 0)  //replace this line

 

$row_count = mysqli_num_rows($r); //(line1) with these 2 lines

if (($row_count) > 0) // (line 2)

 

You could also echo $row_count  before the 'if' to see what it has.

Link to comment
Share on other sites

Thanks much for this troubleshooting step JohnD!  Ok I tried it, and it did return back a return count of 0 for the rows.  So the disconnect is between me clicking the Add to Cart button on the Kona Decaf item and the CALL to the "CALL add_to_cart('$uid', '$sp_type', $pid, 1)");  So I echo'd out the variables used in the add_to_cart stored proc and they returned back correctly(see below).  It can't be the level of security of my cookies, I don't believe?  What else could I try?

What was echo'd out.

$r = mysqli_query($dbc, "CALL get_shopping_cart_contents('$uid')");

$row_count = mysqli_num_rows($r);
echo $row_count;
echo $uid;
echo $sp_type;
echo $pid;


Results from echo'ing out these variables used in the add_to_cart stored proc:

04fbc2b4445cceae1478d7de712f5659ccoffee4

So it came to $row_count = 0
$uid = 
4fbc2b4445cceae1478d7de712f5659c
sp_type = coffee
pid = 4


    

Link to comment
Share on other sites

Thank you Larry!  I didn't think about this until this caused me to look at my earlier post.  The post I posted at 2:44pm yesterday has the session id of:  SESSION] => 4fbc2b4445cceae1478d7de712f5659c

But the session id after closing out the tab from yesterday and starting a new session this morning, they appear to have the same session id.  They can't be right? And shouldn't be stored in the cart table?

This is from this morning:

Results from echo'ing out these variables used in the add_to_cart stored proc:

4fbc2b4445cceae1478d7de712f5659c

Link to comment
Share on other sites

If you're on localhost, they would almost certainly have the same session ID. Not surprising. I was wondering if the same session ID is being used on every page (i.e., new sessions aren't being started). And have you confirmed what session value is being stored in the database? You may need to check the column's size restriction, too (i.e., check the last few characters, not just the first few).

Link to comment
Share on other sites

1. Thank you Larry.  Ah okay, yes I am.  To check the session id would I have to place this in the cart.php file?:  session_start();    

echo session_id();  

2.  And have you confirmed what session value is being stored in the database?  This would be in the cart table?  Nothing is getting populated in the cart table :(

3.  
You may need to check the column's size restriction, too - Ok it was char32 I changed it to char 100.  Let me try this again and see what it does.

 
Link to comment
Share on other sites

Just dropped and recreated the get shopping cart contents stored proc.  And that didn't help, thought maybe it was corrupted some way.  I cleared all cache sense the beginning of time on laptop and tried to add to cart and it still isn't adding the item to the cart.  
But of course it did give me a different UID this time:  f8f221f37c9901c9c53d2dd605f19b72.  Any ideas anyone?

Link to comment
Share on other sites

Ok Larry/guys I got it!  I dropped the add_to_cart stored proc and recreated it with an increased character acceptance from 32 to 100 for the uid.  So the problem was with the stored proc character limit.  I already updated the cart table previously to reflect the same :
 

DELIMITER $$
CREATE PROCEDURE add_to_cart (uid CHAR(100), type VARCHAR(6), pid MEDIUMINT, qty TINYINT)
BEGIN
DECLARE cid INT;
SELECT id INTO cid FROM carts WHERE user_session_id=uid AND product_type=type AND product_id=pid;
IF cid > 0 THEN
UPDATE carts SET quantity=quantity+qty, date_modified=NOW() WHERE id=cid;
ELSE 
INSERT INTO carts (user_session_id, product_type, product_id, quantity) VALUES (uid, type, pid, qty);
END IF;
END$$
DELIMITER ;
Link to comment
Share on other sites

 Share

×
×
  • Create New...