Samza Posted April 30, 2013 Share Posted April 30, 2013 (edited) Hey guys and girls, I am currently following a book to create a PHP shopping cart and I am having trouble passing the data from a form to my cart script that add's the product to the cart. This is the products.php file which contains the form & action which is posting the data from the input forms 'product_qty', 'product_id', and 'req=add': <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td colspan="2"> <a href="/newagedclothing/content/products.php?req=view&product_id=<?php echo $product_id; ?>"> <?php echo $product_title; ?> </a> </td> </tr> <tr> <td width="16%" align="left" valign="top"> <strong>Description:</strong></td> <td width="84%"> <?php echo $caption; ?> </td> </tr> <tr> <td align="left" valign="top"><strong>Price:</strong></td> <td> <?php echo $product_price; ?> </td><?php echo $product_id; ?> </tr> <tr> <td> </td> <td><form method="post" action="index.php?content=cart"> <strong>Add To Cart</strong> <input name="product_qty" type="text" value="1" size="2" /> <input type="hidden" name="req" value="add" /> <input type="hidden" name="product_id" value="<?php echo $product_id; ?>" /> <input type="submit" name="Submit" value="Go!" /> </form> </td> </tr> </table> <hr size="1"> This is the cart.php file: <?php $cart = &new ShoppingCart; $cart_id = $cart->get_cart_id(); switch($_REQUEST['req']){ case "add": $add2cart = $cart->cart_add($_REQUEST['product_id'], $_REQUEST['product_qty']); if(!$add2cart){ echo "<center>The product could not be ". "to your shopping cart. You may ". "have entered an invalid quantity. </center>" . $add2cart; } else { echo "<center>Item added to your shopping cart!<br />". "<a href=\"/cart.php\">View ". "your cart</a></center>"; } break; case "update": while(list($product_id, $qty) = each($_POST[qty])){ $sql = mysql_query("SELECT * FROM shopping_products WHERE product_id='$product_id'"); $row = mysql_fetch_assoc($sql); if($qty == 0){ mysql_query("DELETE FROM shopping_cart WHERE cart_identifier='$cart_id' AND product_id='$product_id'"); } if($qty > $row[product_qty]){ mysql_query("UPDATE shopping_cart SET product_qty='{$row[product_qty]}' WHERE cart_identifier='$cart_id' AND product_id='$product_id'"); $error = TRUE; $products[$product_id] = stripslashes($row[product_title]); } else { mysql_query("UPDATE shopping_cart SET product_qty='$qty' WHERE cart_identifier='$cart_id' AND product_id='$product_id'"); } } if($error){ echo "<center>You have selected more than our current stock for the following product(s): <br />"; while(list($product_id, $product_title) = each($products)){ echo "<a href=\"/products.php?req=view&product_id=$product_id\">$product_title</a><br />"; } echo "<br />"; echo "We have updated your quantity to the maximum value that we have in stock.</center><br />"; echo "<center><a href=\"/cart.php\">Back to cart</a></center>"; } else { header ("Location: /cart.php"); } break; case "remove": $sql = mysql_query("DELETE FROM shopping_cart WHERE cart_identifier='$cart_id' AND product_id='{$_REQUEST['product_id']}'"); header("Location: /cart.php"); break; case "empty_confirm": echo "<center>Are you sure you want to empty your cart?<br />". "<a href=\"cart.php?req=empty\">Yes</a>". " | ". "<a href=\"/cart.php\">No</a></center>"; break; case "empty": $cart->empty_cart(); echo "<center>Your cart has been emptied!</center>"; break; default: if($cart_id){ $num_items = mysql_result(mysql_query("SELECT COUNT(*) as items FROM shopping_cart WHERE cart_identifier='$cart_id'"),0); if($num_items == 0){ echo "<center>Your shopping cart is empty!</center>"; exit(); } } else { echo "<center>Your shopping cart is empty</center>"; exit; } ?> <p>Your shopping cart</p> <p>This page allows you to modify or empty your shopping cart contents. Simply change the number of each product you wish to purchase and select the "update cart" link at the bottom.</p> <form name="update" method="post" action="/cart.php"> <table> <tr> <td>Qty</td> <td>Product</td> <td align="right">Price</td> <td align="right">Product Total</td> </tr> <?php $total = mysql_result(mysql_query("SELECT sum(product_qty * product_price) AS subtotal FROM shopping_cart WHERE cart_identifier='$cart_id'"), 0); $total = number_format($total, 2); $sql = mysql_query("SELECT * FROM shopping_cart WHERE cart_identifier='$cart_id'"); while ($row = mysql_fetch_array($sql)) { $product_total = number_format(($row[product_qty] * $row[product_price]), 2); echo "<tr><td><input type=\"text\" " . "name=\"qty[$row[product_id]]\" " . "size=\"2\" value=\"$row[product_qty]\" />" . "<br /><a href=\"/cart.php?req=remove&" . "product_id=$row[product_id]\">" . "Remove</a>" . "</td>" . "<td><a href=\"/products.php?req=view&product_id=$row[product_id]\">" . stripslashes($row[product_title]) . "</a></td>" . "<td align=\"right\">\$$row[product_price]</td>" . "<td align=\"right\">\$$product_total</td>" . "</tr>"; } ?> <tr> <td colspan="2"> </td> <td align="right">Total:</td> <td align="right">$<?=$total ?>< /td> </tr> <tr> <td colspan="4" align="center"> <a href="javascript:void(document.update.submit())">Update Cart</a> | <a href="/cart.php?req=empty_confirm">Empty Cart</a> | <a href="/products.php">Continue shopping</a> | <a href="/checkout.php">Checkout</a> </td> </tr> </table> </form> <?php break; } ?> Here is the add_cart function: function cart_add($product_id, $product_qty){ $cart_id = $this->get_cart_id(); if(!$cart_id){ //if no cart id found, generate one $unique_cid = md5(uniqid(rand(),1)); //set cart id into cookie setcookie('cid', $unique_cid, time()+24*3600*60); //Register session with cart id value $_SESSION['cid'] = $unique_cid; //if persion is a member //modify their profile with //cart id in the database if($_SESSION['login']){ $_SESSION['cid'] = $unique_cid; @mysql_query("UPDATE members SET cart_id='$unique_cid' WHERE id='".$_SESSION['userid']."'"); } }//end generate unique id $sql_get_product = mysql_query("SELECT * FROM shopping_products WHERE product_id='$product_id'"); $sql_check = mysql_query("SELECT * FROM shopping_cart WHERE cart_identifier='{$_SESSION['cid']}' AND product_id='$product_id'"); while($row = mysql_fetch_array($sql_check)){ $products = mysql_fetch_assoc($sql_get_product); if(($product_qty + $products[product_qty]) > $products[product_qty]){ $new_qty = $products[product_qty]; } else { $new_qty = ($product_qty + $row[product_qty]); } $sql = mysql_query("UPDATE shopping_cart SET product_qty='$new_qty', date = now() WHERE id='{$row['id']}'"); $skip = TRUE; } if(!$skip){ $products = mysql_fetch_assoc($sql_get_product); if($products[product_qty] < $product_qty){ $product_qty = $products[product_qty]; } if($product_qty > 0){ $sql = mysql_query("INSERT INTO shopping_cart (cart_identifier, product_id, product_title, product_qty, product_price, date) VALUES ('{$_SESSION['cid']}', '$product_id', '{$products['product_title']}', '$product_qty','{$products['product_price']}', now())"); } else { $sql = FALSE; } } if(!$sql){ return FALSE; } else { return TRUE; } }//end cart_add() My problem is that whenever I click the 'add' button to send the data across it seems like it isn't being sent correctly. I'm am a bit unsure on how to structure the url for the submit button and I think it's to do with that. These pages are being controlled around my index.php file and the products page url looks like this, 'index.php?content=products'. All my content files are in a folder named 'content'. Please ask for any other questions to aid my assistance. Much appreciated ! Sam Edited April 30, 2013 by Samza Quote Link to comment https://forums.phpfreaks.com/topic/277462-php-shopping-cart-advice/ Share on other sites More sharing options...
cyberRobot Posted May 1, 2013 Share Posted May 1, 2013 My problem is that whenever I click the 'add' button to send the data across it seems like it isn't being sent correctly. I'm am a bit unsure on how to structure the url for the submit button and I think it's to do with that. Note that since the form is using the POST method, the information isn't going to be visible in the URL. It's passed behind the scenes. Are you getting any errors? Did you look at the source code (in your browser) for the form to see if the values have been populated? Did you check to see if the information was passed to cart.php? To do that you can use something like var_dump() to display the POST variables. Quote Link to comment https://forums.phpfreaks.com/topic/277462-php-shopping-cart-advice/#findComment-1427491 Share on other sites More sharing options...
Samza Posted May 1, 2013 Author Share Posted May 1, 2013 Hi cyberRobot, Yes I echo out the variables to check if cart.php was receiving the data which it is. I now believe it has something to do with this line in cart.php - the output is dropping into the if(!$add2cart) which clear thinks it is FALSE. $add2cart = $cart->cart_add($_REQUEST['product_id'], $_REQUEST['product_qty']); This means the underlying issue is in the function named cart_add in clsShoppingCart.php file however I am unsure on how I can test this part as it gets quiet complicated from line 24. while($row = mysql_fetch_array($sql_check)){ $products = mysql_fetch_assoc($sql_get_product); if(($product_qty + $products[product_qty]) > $products[product_qty]){ $new_qty = $products[product_qty]; } else { $new_qty = ($product_qty + $row[product_qty]); } $sql = mysql_query("UPDATE shopping_cart SET product_qty='$new_qty', date = now() WHERE id='{$row['id']}'"); $skip = TRUE; } if(!$skip){ $products = mysql_fetch_assoc($sql_get_product); if($products[product_qty] < $product_qty){ $product_qty = $products[product_qty]; } if($product_qty > 0){ $sql = mysql_query("INSERT INTO shopping_cart (cart_identifier, product_id, product_title, product_qty, product_price, date) VALUES ('{$_SESSION['cid']}', '$product_id', '{$products['product_title']}', '$product_qty','{$products['product_price']}', now())"); } else { $sql = FALSE; } } if(!$sql){ return FALSE; } else { return TRUE; } }//end cart_add() Quote Link to comment https://forums.phpfreaks.com/topic/277462-php-shopping-cart-advice/#findComment-1427534 Share on other sites More sharing options...
Solution Samza Posted May 2, 2013 Author Solution Share Posted May 2, 2013 I have also checked to see the data in the variables below; $sql_get_products echo's nothing and $sql_check echo's 'Resource id #11'. Quote Link to comment https://forums.phpfreaks.com/topic/277462-php-shopping-cart-advice/#findComment-1427774 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.