smileyrva Posted September 6, 2010 Share Posted September 6, 2010 // does the product exist ? $sql = "SELECT pd_id, pd_qty FROM tbl_product WHERE pd_id = $productId"; $result = dbQuery($sql); if (dbNumRows($result) != 1) { // the product doesn't exist header('Location: cart.php'); } else { // how many of this product we // have in stock $row = dbFetchAssoc($result); $currentStock = $row['pd_qty']; if ($currentStock == 0) { // we no longer have this product in stock // show the error message setError('The product you requested is no longer in stock'); header('Location: cart.php'); exit; } } // current session id $sid = session_id(); session_register("size"); $size = $_SESSION['size']; // check if the product is already // in cart table for this session $sql = "SELECT pd_id FROM tbl_cart WHERE pd_id = $productId AND ct_session_id = '$sid'"; $result = dbQuery($sql); if (dbNumRows($result) == 0) { // put the product in cart table $sql = "INSERT INTO tbl_cart (pd_id, ct_qty, size, ct_session_id, ct_date) " . "VALUES ($productId, 1, '$size', '$sid', NOW())"; $result = dbQuery($sql); } else { // update product quantity in cart table $sql = "UPDATE tbl_cart SET ct_qty = ct_qty + 1 WHERE ct_session_id = '$sid' AND pd_id = $productId"; $result = dbQuery($sql); } This is just a piece of my cartfunctions.php (which is an include() once you add an item to the cart). I've been trouble shooting this all day and finally got it to stop giving me error messages. Im trying to allow the user to pick a size between 4 options, after that it is added to the cart and I can easily find out what size shirt the customer wants before I ship it. but now it wont update the $size variable to the DB. Am I defining this wrong or maybe my whole approach is messed up? If you would like to see the setup you can go to rbcrime.com/newshop/newshop/ . Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/ Share on other sites More sharing options...
Hypnos Posted September 7, 2010 Share Posted September 7, 2010 Code looks right. Try echoing the query and then copy and paste it into mysql directly. See if you get an error. Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108079 Share on other sites More sharing options...
smileyrva Posted September 7, 2010 Author Share Posted September 7, 2010 That was the functions that are called upon after you add to cart but maybe the problem lies in my productdetail.php? would give the answer. I still cannot figure this out and its driving me crazzzzyyyyy. lol. if anyone can help I would really appreciate it, thanks! <?php if (!defined('WEB_ROOT')) { exit; } $product = getProductDetail($pdId, $catId); // we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url extract($product); ?> <table width="100%" border="0" cellspacing="0" cellpadding="10"> <tr> <td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>"></td> <td valign="middle"> <strong><?php echo $pd_name; ?></strong><br> Price : <?php echo displayAmount($pd_price); ?><br> <?php // if we still have this product in stock // show the 'Add to cart' button if ($pd_qty > 0) { ?> <br> <br> Size: <select size="1" name="size"> <option value="Small">Small</option> <option>Medium</option> <option>Large</option> <option>Extra Large</option> </select> <br> <input type="button" name="btnAddToCart" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton"> <?php } else { echo 'Out Of Stock'; } ?> </td> </tr> <tr align="left"> <td colspan="2"><?php echo $pd_description; ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108446 Share on other sites More sharing options...
smileyrva Posted September 7, 2010 Author Share Posted September 7, 2010 I just tried to see if I could echo the query...I tried displaying the size in cart.php and im getting an undefined variable error. Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108461 Share on other sites More sharing options...
wildteen88 Posted September 7, 2010 Share Posted September 7, 2010 One issue I see is in your form you're not setting any values for the sizes field <select size="1" name="size"> <option value="Small">Small</option> <option>Medium</option> <option>Large</option> <option>Extra Large</option> </select> You're deifing the labels but no values! For each option (<option></option>) you need to set a value. You've only set a value for the first option. To get the selected value you'd use either $_POST['size'] or $_GET['size'] (depending on your forms submit method). Also when setting session variables do not use the session_register function. session_register("size"); $size = $_SESSION['size']; To set a session variable you'd use $_SESSION['session_variable_name'] = 'some value'; To set thr size session you'd use $_SESSION['size'] = $size; Just remember to call session_start at the top of all pages that uses sessions. Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108481 Share on other sites More sharing options...
smileyrva Posted September 8, 2010 Author Share Posted September 8, 2010 So if my form is method=post how can i define that on the next page? Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108502 Share on other sites More sharing options...
wildteen88 Posted September 8, 2010 Share Posted September 8, 2010 If your forms submit method is POST, then you'd get the selected size from the $_POST['size'] variable $size = $_POST['size']; To assign the chosen size to the session you'd do $_SESSION['size'] = $size; To get the size from the session you'd use the $_SESSION['size'] variable. Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108506 Share on other sites More sharing options...
smileyrva Posted September 8, 2010 Author Share Posted September 8, 2010 im still getting an undefined variable error. if it makes it easier this is the phpwebcommerce cart (plaincart?). Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108509 Share on other sites More sharing options...
smileyrva Posted September 8, 2010 Author Share Posted September 8, 2010 Can anyone please help? I just need a simple dropdown box with 4 choices (IE: Small, Med, Large, XL) that can be defined as a variable to be added in a DB. It seems easy but it is kicking my butt. It is the ww.phpwebcommerce.com cart if it makes it easier. Any help would be greatly appreciated, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/212708-problem-defining-session/#findComment-1108860 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.