twilitegxa Posted July 16, 2009 Share Posted July 16, 2009 The shopping cart I have is not adding the items to the table. Can someone help me figure out why? Here is my script for showing the item for my shopping cart: <?php session_start(); //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); $display_block = "<h1>My Store - Item Detail</h1>"; //validate item $get_item = "select c.cat_title, si.item_title, si.item_price, si.item_desc, si.item_image from store_items as si left join store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]"; $get_item_res = mysql_query($get_item) or die(mysql_error()); $item_id = $_GET['item_id']; if (mysql_num_rows($get_item_res) < 1) { //invalid item $display_block .= "<p><em>Invalid item selection.</em></p>"; } else { //valid item, get info $cat_title = strtoupper(stripslashes( mysql_result($get_item_res,0,'cat_title'))); $item_title = stripslashes(mysql_result($get_item_res,0, 'item_title')); $item_price = mysql_result($get_item_res,0, 'item_price'); $item_desc = stripslashes(mysql_result($get_item_res,0, 'item_desc')); $item_image = mysql_result($get_item_res,0, 'item_image'); //make breadcrumb trail $display_block .= "<p><strong>You are viewing:</em><br> <a href=\"seestore.php?cat_id\">$cat_title</a> > $item_title</strong></p> <table cellpadding=3 cellspacing=3> <tr> <td valign=middle align=center><img src=\"$item_image\" style=\"margin: 100; display: block;\"></td> <td valign=middle><p><strong>Description:</strong><br>$item_desc</p> <p><strong>Price:</strong> \$$item_price</p> <form method=post action=\"addtocart.php\">"; //get colors $get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color"; $get_colors_res = mysql_query($get_colors) or die(mysql_error()); if (mysql_num_rows($get_colors_res) > 0) { $display_block .= "<p><strong>Available Colors:</strong> <select name=\"sel_item_color\">"; while ($colors = mysql_fetch_array($get_colors_res)) { $item_color = $colors['item_color']; $display_block .= "<option value=\"$item_color\">$item_color</option>"; } $display_block .= "</select>"; } //get sizes $get_sizes = "select item_size from store_item_size where item_id = $item_id order by item_size"; $get_sizes_res = mysql_query($get_sizes) or die(mysql_error()); if (mysql_num_rows($get_sizes_res) > 0) { $display_block .= "<p><strong>Available Sizes:</strong> <select name=\"sel_item_size\">"; while ($sizes = mysql_fetch_array($get_sizes_res)) { $item_size = $sizes['item_size']; $display_block .= " <option value=\"$item_size\">$item_size</option>"; } $display_block .= "</select>"; } $display_block .= " <p><strong>Select Quantity:</strong> <select name=\"sel_item_qty\">"; for($i=1; $i<11; $i++) { $display_block .= "<option value=\"$i\">$i</option>"; } $display_block .= " </select> <input type=\"hidden\" name=\"sel_item_id\" value=\"$_GET[item_id]\"> <p><input type=\"submit\" name=\"submit\" value=\"Add To Cart\"></p> </form> </td> </tr> </table>"; } ?> <html> <head> <title>My Store</title> </head> <body> <?php print $display_block; ?> </body> </html> And here is the code that displays the shopping cart: <?php session_start(); //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); $display_block = "<h1>Your Shopping Cart</h1>"; //check for cart items based on user id $PHPSESSID = session_id(); $get_cart = "select st.id, si.item_title, si.item_price, st.sel_item_qty, st.sel_item_size, st.sel_item_color from store_shoppertrack as st left join store_items as si on si.id = st.sel_item_id where session_id = '$PHPSESSID'"; $get_cart_res = mysql_query($get_cart) or die(mysql_error()); if (mysql_num_rows($get_cart_res) < 1) { //print message $display_block .= "<p>You have no items in your cart. Please <a href=\"seestore.php\">continue to shop</a>!</p>"; } else { //get info and build cart display $display_block .= " <table cellpadding=3 cellspacing=2 border=1 width=98%> <tr> <th>Title</th> <th>Size</th> <th>Color</th> <th>Price</th> <th>Qty</th> <th>Total Price</th> <th>Action</th> </tr>"; while ($cart = mysql_fetch_array($get_cart_res)) { $id = $cart['id']; $item_title = stripslashes($cart['item_title']); $item_price = $cart['item_price']; $item_qty = $cart['item_qty']; $item_color = $cart['sel_item_color']; $item_size = $cart['sel_item_size']; $total_price = sprintf("%.02f", $item_price * $item_qty); $display_block .= "<tr> <td align=center>$item_title <br></td> <td align=center>$item_size <br></td> <td align=center>$item_color <br></td> <td align=center>$item_price <br></td> <td align=center>$item_qty <br></td> <td align=center>\$ $total_price</td> <td align=center><a href=\"removefromcart.php?id=$id\">remove</a></td> </tr>"; } $display_block .= "</table>"; } ?> <html> <head> <title>My Store</title> </head> <body> <?php print $display_block; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/ Share on other sites More sharing options...
twilitegxa Posted July 16, 2009 Author Share Posted July 16, 2009 And here is the script for adding to the cart: <?php session_start(); //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); if ($_POST[sel_item_id] != "") { //validate item and get title and price $get_iteminfo = "select item_title from store_items where id = $_POST[sel_item_id]"; $get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error()); if (mysql_num_rows($get_iteminfo_res) < 1) { //invalid id, send away header("Location: seestore.php"); exit; } else { //get info $item_title = mysql_result($get_iteminfo_res,0, 'item_title'); //add info to cart table $addtocart = "insert into store_shoppertrack values ('', '$PHPSESSID', '$_POST[sel_item_id]', '$_POST[sel_item_qty]', '$_POST[sel_item_size]', '$_POST[sel_item_color]', now()"; mysql_query($addtocart); //redirect to showcart page header("Location: showcart.php"); exit; } } else { //send them somewhere else header("Location: seestore.php"); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/#findComment-876705 Share on other sites More sharing options...
PFMaBiSmAd Posted July 16, 2009 Share Posted July 16, 2009 You have got to use error checking logic in your code (everywhere and every time) to get it to tell you what it is or is not doing, even if you just output a message that the "query to add to the cart failed" so that you know what and where something failed. Because, if your code does not have any logic in it to tell you what and where something failed, what makes you think someone in a help forum can? Change - mysql_query($addtocart); to - if(mysql_query($addtocart)){ // the INSERT query worked - //redirect to showcart page header("Location: showcart.php"); exit; } else { // the INSERT query failed, DO SOMETHING to let the visitor know echo "Could not execute query to add product to cart"; // and do something when debugging, like show the mysql_error() output die (mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/#findComment-876716 Share on other sites More sharing options...
twilitegxa Posted July 16, 2009 Author Share Posted July 16, 2009 I changed the code to: <?php session_start(); //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); if ($_POST['sel_item_id'] != "") { //validate item and get title and price $get_iteminfo = "select item_title from store_items where id = $_POST[sel_item_id]"; $get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error()); if (mysql_num_rows($get_iteminfo_res) < 1) { //invalid id, send away header("Location: seestore.php"); exit; } else { //get info $item_title = mysql_result($get_iteminfo_res,0, 'item_title'); //add info to cart table $addtocart = "insert into store_shoppertrack values ('', '$PHPSESSID', '$_POST[sel_item_id]', '$_POST[sel_item_qty]', '$_POST[sel_item_size]', '$_POST[sel_item_color]', now()"; if(mysql_query($addtocart)){ // the INSERT query worked - //redirect to showcart page header("Location: showcart.php"); exit; } else { // the INSERT query failed, DO SOMETHING to let the visitor know echo "Could not execute query to add product to cart"; // and do something when debugging, like show the mysql_error() output die (mysql_error()); } //redirect to showcart page header("Location: showcart.php"); exit; } } else { //send them somewhere else header("Location: seestore.php"); exit; } ?> as suggested, but now I get these errors: Notice: Undefined variable: PHPSESSID in C:\wamp\www\addtocart.php on line 26 Notice: Undefined index: sel_item_size in C:\wamp\www\addtocart.php on line 27 Notice: Undefined index: sel_item_color in C:\wamp\www\addtocart.php on line 27 Could not execute query to add product to cartYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 How do I set these indexes? Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/#findComment-876745 Share on other sites More sharing options...
pengu Posted July 17, 2009 Share Posted July 17, 2009 I'd recommend reading the errors and doing as they say. One of your variables isn't defined at all. Is $PHPSESSID suppose to be a session thing? $_SESSION['id'] maybe? If not, it's not defined. Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/#findComment-876816 Share on other sites More sharing options...
twilitegxa Posted July 17, 2009 Author Share Posted July 17, 2009 Well, I am doing this from a tutorial in a book, so I'm not completely sure. Usually when I do a tutorial in these books, after I get the code working, I play around with it more and learn how it works and what things are for (at least some of the things), and this happens to be one that I don't understand yet. I will post all the pages for this script in succession so you can see where the code appears to be going, so maybe you can see what I have done wrong and why. Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/#findComment-876830 Share on other sites More sharing options...
twilitegxa Posted July 17, 2009 Author Share Posted July 17, 2009 seestore.php <?php //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); $display_block = "<h1>My Categories</h1> <p>Select a category to see its items.</p>"; //show categories first $get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) < 1) { $display_block = "<p><em>Sorry, no categories to browse.</em></p>"; } else { while ($cats = mysql_fetch_array($get_cats_res)) { $cat_id = $cats['id']; $cat_title = strtoupper(stripslashes($cats['cat_title'])); $cat_desc = stripslashes($cats['cat_desc']); $display_block .= "<p><strong><a href=\"$_SERVER[php_SELF]?cat_id=$cat_id\">$cat_title</a></strong> <br>$cat_desc</p>"; if(isset($_GET['cat_id']) && $_GET['cat_id'] == $cat_id) { //get items $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title"; $get_items_res = mysql_query($get_items) or die(mysql_error()); if (mysql_num_rows($get_items_res) < 1) { $display_block = "<p><em>Sorry, no items in this category.</em></p>"; } else { $display_block .= "<ul>"; while ($items = mysql_fetch_array($get_items_res)) { $item_id = $items['id']; $item_title = stripslashes($items['item_title']); $item_price = $items['item_price']; $display_block .= "<li><a href=\"showitem.php?item_id=$item_id\">$item_title</a> </strong> (\$$item_price)"; } $display_block .= "</ul>"; } } } } ?> <html> <head> <title>My Categories</title> </head> <body> <?php print $display_block; ?> </body> </html> showitem.php <?php session_start(); //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); $display_block = "<h1>My Store - Item Detail</h1>"; //validate item $get_item = "select c.cat_title, si.item_title, si.item_price, si.item_desc, si.item_image from store_items as si left join store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]"; $get_item_res = mysql_query($get_item) or die(mysql_error()); $item_id = $_GET['item_id']; if (mysql_num_rows($get_item_res) < 1) { //invalid item $display_block .= "<p><em>Invalid item selection.</em></p>"; } else { //valid item, get info $cat_title = strtoupper(stripslashes( mysql_result($get_item_res,0,'cat_title'))); $item_title = stripslashes(mysql_result($get_item_res,0, 'item_title')); $item_price = mysql_result($get_item_res,0, 'item_price'); $item_desc = stripslashes(mysql_result($get_item_res,0, 'item_desc')); $item_image = mysql_result($get_item_res,0, 'item_image'); //make breadcrumb trail $display_block .= "<p><strong>You are viewing:</em><br> <a href=\"seestore.php?cat_id\">$cat_title</a> > $item_title</strong></p> <table cellpadding=3 cellspacing=3> <tr> <td valign=middle align=center><img src=\"$item_image\" style=\"margin: 100; display: block;\"></td> <td valign=middle><p><strong>Description:</strong><br>$item_desc</p> <p><strong>Price:</strong> \$$item_price</p> <form method=post action=\"addtocart.php\">"; //get colors $get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color"; $get_colors_res = mysql_query($get_colors) or die(mysql_error()); if (mysql_num_rows($get_colors_res) > 0) { $display_block .= "<p><strong>Available Colors:</strong> <select name=\"sel_item_color\">"; while ($colors = mysql_fetch_array($get_colors_res)) { $item_color = $colors['item_color']; $display_block .= "<option value=\"$item_color\">$item_color</option>"; } $display_block .= "</select>"; } //get sizes $get_sizes = "select item_size from store_item_size where item_id = $item_id order by item_size"; $get_sizes_res = mysql_query($get_sizes) or die(mysql_error()); if (mysql_num_rows($get_sizes_res) > 0) { $display_block .= "<p><strong>Available Sizes:</strong> <select name=\"sel_item_size\">"; while ($sizes = mysql_fetch_array($get_sizes_res)) { $item_size = $sizes['item_size']; $display_block .= " <option value=\"$item_size\">$item_size</option>"; } $display_block .= "</select>"; } $display_block .= " <p><strong>Select Quantity:</strong> <select name=\"sel_item_qty\">"; for($i=1; $i<11; $i++) { $display_block .= "<option value=\"$i\">$i</option>"; } $display_block .= " </select> <input type=\"hidden\" name=\"sel_item_id\" value=\"$_GET[item_id]\"> <p><input type=\"submit\" name=\"submit\" value=\"Add To Cart\"></p> </form> </td> </tr> </table>"; } ?> <html> <head> <title>My Store</title> </head> <body> <?php print $display_block; ?> </body> </html> addtocart.php <?php session_start(); //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); if ($_POST['sel_item_id'] != "") { //validate item and get title and price $get_iteminfo = "select item_title from store_items where id = $_POST[sel_item_id]"; $get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error()); if (mysql_num_rows($get_iteminfo_res) < 1) { //invalid id, send away header("Location: seestore.php"); exit; } else { //get info $item_title = mysql_result($get_iteminfo_res,0, 'item_title'); //add info to cart table $addtocart = "insert into store_shoppertrack values ('', '$PHPSESSID', '$_POST[sel_item_id]', '$_POST[sel_item_qty]', '$_POST[sel_item_size]', '$_POST[sel_item_color]', now()"; if(mysql_query($addtocart)){ // the INSERT query worked - //redirect to showcart page header("Location: showcart.php"); exit; } else { // the INSERT query failed, DO SOMETHING to let the visitor know echo "Could not execute query to add product to cart"; // and do something when debugging, like show the mysql_error() output die (mysql_error()); } } } else { //send them somewhere else header("Location: seestore.php"); exit; } ?> When I go to click the submit button to add the item to the cart, it doesn't show anything in the status bar to show where the submit button takes you and the following errors present: Notice: Undefined variable: PHPSESSID in C:\wamp\www\addtocart.php on line 26 Notice: Undefined index: sel_item_size in C:\wamp\www\addtocart.php on line 27 Notice: Undefined index: sel_item_color in C:\wamp\www\addtocart.php on line 27 Could not execute query to add product to cartYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 Any suggestions? Link to comment https://forums.phpfreaks.com/topic/166247-items-not-getting-added-to-table-for-shopping-cart-script/#findComment-876832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.