php - Notice: Undefined index on line 5 -
php - Notice: Undefined index on line 5 -
i getting next error on php & mysql application murach's php , mysql book:
notice: undefined index: category_id in c:\xampp\htdocs\book_apps\ch04_product_viewer\index.php on line 5 i didn't modified code whatsoever (yet), assumed should work out of box. application displays products database should, problem annoying error.
here php code of index.php file:
<?php require 'database.php'; // category id $category_id = $_get['category_id']; if (!isset($category_id)) { $category_id = 1; } // name current category $query = "select * categories categoryid = $category_id"; $category = $db->query($query); $category = $category->fetch(); $category_name = $category['categoryname']; // categories $query = 'select * categories order categoryid'; $categories = $db->query($query); // products selected category $query = "select * products categoryid = $category_id order productid"; $products = $db->query($query); ?>
it's notice rather error, meaning php tells thinks wrong, code executed unhindered. reason if don't pass category_id in query string, corresponding element in array not exist.
there lot of legacy php code doesn't check existence of array elements - in cases, it's unavoidable alter error reporting level mute notices. can argued accessing parameter shouldn't trigger kind of notice @ all.
however, adjusting error reporting level regarded bad practice, notices can useful. when writing new code, add together necessary checks doesn't happen.
the proper way without getting notice be
if (isset($_get['category_id'])) $category_id = $_get['category_id']; php mysql html database
Comments
Post a Comment