hass1980 Posted April 15, 2009 Share Posted April 15, 2009 Hi Im getting this error on my web page Notice: Undefined index: id in C:\wamp\www\website\seestore.php on line 23 <?php //connect to database $conn = mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("shoppingcart",$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, name, description from categories order by name"; $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['name'])); $cat_desc = stripslashes($cats['description']); $display_block .= "<p><strong><a href=\"$_SERVER[php_SELF]?id=$cat_id\">$cat_title</a></strong><br>$cat_desc</p>"; if ($_GET['id'] == $cat_id) { //get items $get_items = "select id, name, price from store_items where id = $cat_id order by name"; $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['name']); $item_price = $items['price']; $display_block .= "<li><a href=\"showitem.php?id=$item_id\">$item_title</a></strong> (\$$item_price)"; } $display_block .= "</ul>"; } } } } ?> <HTML> <HEAD> <TITLE>My Categories</TITLE> </HEAD> <BODY> <? print $display_block; ?> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/154252-undefined-index/ Share on other sites More sharing options...
Maq Posted April 15, 2009 Share Posted April 15, 2009 What does your URI look like when you see that error? I'm guessing that 'id' is never being set the first time you run your script. You should check it with isset() to make sure it as a value, or is even in the URI, before you compare it to anything. Quote Link to comment https://forums.phpfreaks.com/topic/154252-undefined-index/#findComment-810949 Share on other sites More sharing options...
hass1980 Posted April 15, 2009 Author Share Posted April 15, 2009 my URI is http://localhost/website/seestore.php where abouts do i put that code you stated? im new to this language Im just following an example out of a book. Quote Link to comment https://forums.phpfreaks.com/topic/154252-undefined-index/#findComment-810953 Share on other sites More sharing options...
Maq Posted April 15, 2009 Share Posted April 15, 2009 This checks to see if the id exists in the URL, if it does assign $id to it, if not give $id an empty string. $id = isset($_GET['id']) ? $_GET['id'] : ""; if ($_GET['id'] == $cat_id) { Quote Link to comment https://forums.phpfreaks.com/topic/154252-undefined-index/#findComment-810963 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.