twilitegxa Posted July 10, 2009 Share Posted July 10, 2009 I am getting the following error on my php script: Notice: Undefined variable: PHPSESSID in C:\wamp\www\showcart.php on line 14 Here is my code: <?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 $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> <? print $display_block; ?> </body> </html> How would I define this variable? Can anyone help? Quote Link to comment Share on other sites More sharing options...
ignace Posted July 10, 2009 Share Posted July 10, 2009 $PHPSESSID = session_id(); after session_start(); Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 Thanks, that seemed to fix that problem. :-) But now my page is displaying a blank page. :-( Can anyone see why? :-( <?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> <? print $display_block; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 10, 2009 Author Share Posted July 10, 2009 Here is the script that sends the user to this page that is displaying the blank page (showcart.php): 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()"; mysql_query($addtocart); //redirect to showcart page header("Location: showcart.php"); exit; } } else { //send them somewhere else header("Location: seestore.php"); exit; } ?> In case I am missing something there. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 remove or die() Quote Link to comment Share on other sites More sharing options...
kemo Posted July 11, 2009 Share Posted July 11, 2009 remove or die() Don't he better check if selected database exists then remove "or die()" ? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 11, 2009 Share Posted July 11, 2009 Do a "view source" in your browser of the blank page. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 12, 2009 Author Share Posted July 12, 2009 I changed the <? to <?php and now the page displays, but the items are not getting added to the cart. It displays the message saying there are no items in the cart. The items are not getting added to the shoppertrack table either. Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 12, 2009 Share Posted July 12, 2009 I personally like having a death function that does a dump when it dies, and when you're done developing it. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 12, 2009 Author Share Posted July 12, 2009 Okay, I don't understand what that means... :-( Quote Link to comment Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 remove or die() Don't he better check if selected database exists then remove "or die()" ? Yep, but not by using or die(). Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 13, 2009 Share Posted July 13, 2009 On the master page I use the function death($sql), so that I can change it from creating an output thing initially to a error reporting with feedback after the application is done, it helps with debugging if they provide documentation well enough. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 16, 2009 Author Share Posted July 16, 2009 Which or die() were you guys telling me to remove? Quote Link to comment Share on other sites More sharing options...
ignace Posted July 16, 2009 Share Posted July 16, 2009 Which or die() were you guys telling me to remove? All of 'em It's a bad habbit tought by newb tutorial writers. Use a combination of set_error_handler() and trigger_error() or grow up and start writing OO flavored with design patterns and use Exception's to handle errors 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.