Undefined index error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hass1980
Forum Newbie
Posts: 1
Joined: Sat Apr 11, 2009 2:52 am

Undefined index error

Post by hass1980 »

Hey guys

I am getting this error Notice: Undefined index: catid in C:\wamp\www\chapter25\index.php on line 24

Here is the code from the index.php

Code: Select all

 
 
 
<?php
//connect to database
$conn = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("book_sc",$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 catid, catname from categories order by catname";
$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['catid'];
        $cat_title = strtoupper(stripslashes($cats['catname']));
       
 
        $display_block .= "<p><strong><a href=\"$_SERVER[PHP_SELF]?catid=$cat_id\">$cat_title</a></strong><br></p>";
 
        if ($_GET['catid'] == $cat_id) {
           //get items
           $get_items = "select catid, title, price from books where cat_id = $cat_id order by 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[catid];
                   $item_title = stripslashes($items[title]);
                   $item_price = $items[price];
 
                   $display_block .= "<li><a href=\"showitem.php?item_id=$item_id\">$item_title</a></strong> (\$$item_price)";
                }
 
                $display_block .= "</ul>";
           }
       }
   }
}
?>
<html>
 
<head>
</head>
 
<body>
<? print $display_block; ?>
</body>
 
</html>
 
 
 
Any ideas?? Thanks :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Undefined index error

Post by requinix »

It means there wasn't a &catid= in the URL.

Use functions like isset or empty to check if it is present.
Post Reply