I am getting the following error on a PHP & MySQL application from Murach's PHP and 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 the code whatsoever (yet), so I assumed it should work out of the box. The application displays products from a database as it should, the only problem is I get this annoying error.
Here is the PHP code of the index.php file:
<?php
require 'database.php';
// Get category ID
$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}
// Get name for current category
$query = "SELECT * FROM categories
WHERE categoryID = $category_id";
$category = $db->query($query);
$category = $category->fetch();
$category_name = $category['categoryName'];
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$categories = $db->query($query);
// Get products for selected category
$query = "SELECT * FROM products
WHERE categoryID = $category_id
ORDER BY productID";
$products = $db->query($query);
?>