shakked
shakked

Reputation: 811

Include function php giving error from old code?

So the problem I am having is with this basic database application exercise I'm doing from a book. Basically, when you try to add a product to the database, as the code below shows, it checks to make sure all the fields are filled out. But then, at the end when it's performed the query and I have the include('home.php'), something is happening that when I am returned to the home.php page and I click one of the links on that page, I am getting the error I would get if one of the fields is left blank. Any ideas?

<?php
// Get the product data
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];

// Validate inputs
if (empty($code) || empty($name) || empty($price) ) {
    $error = "Invalid product data. Check all fields and try again.";
    include('error.php');
} else {
    // If valid, add the product to the database
    require_once('database.php');
    $query = "INSERT INTO products
                 (categoryID, productCode, productName, listPrice)
              VALUES
                 ('$category_id', '$code', '$name', '$price')";
    $db->exec($query);

    // Display the Product List page
   include('home.php') ;
}
?>

home.php without the html/css

<?php
require_once('database.php');

    // Get category ID
    if(!isset($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);


?>

Upvotes: 0

Views: 120

Answers (1)

Joke_Sense10
Joke_Sense10

Reputation: 5402

In the file home.php, your using $_GET instead of $_POST:

Change this:

 $category_id = $_GET['category_id'];

To:

$category_id = $_POST['category_id'];

Upvotes: 1

Related Questions