0

So I haven't the foggiest idea on what needs to be done to get this dropdown menu working. It was working before, but when I made some modifications, it wasn't working anymore. The error message displayed is:

Notice:  Undefined variable: categories in D:\xampp\htdocs\tech_support\product_register\product_register.php on line 19

But I have it defined in my index.php:

<?php

    require('../model/database.php');

    // Get the models needed - work will need to be done in both
    require('../model/customer_db.php');
    require('../model/product_db.php');
    require('../model/registration_db.php');

    $action = filter_input(INPUT_POST, 'action');
    if ($action == NULL) {
        $action = filter_input(INPUT_POST, 'action');
        if ($action == null) {
            $action = 'product_register';
        }
    }

    //When the user clicks the first link on the home page, bring them to the login page.
    if ($action == 'product_register') {
        include('customer_login.php');
    }

    //When the user clicks the login button, the system checks for errors in their typing.
    //If no errors are present, proceed to product_register.php.
    else if ($action == 'login') {
        $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
        if ($email == NULL || $email == FALSE) {
            $error = 'Invalid email. Try again.';
            include('../errors/error.php');
        } else {
            $custEmail = get_email($_POST['email']);
            if ($custEmail) {
                $category_name =  get_product_name($productName);
                $categories = get_products();
                $products = get_products_by_name($name);
                header("Location: product_register.php");
            } else {
                $error = 'Invalid email. Try again.';
                include('../errors/error.php');
            }
        }
    }
?>

and I have it defined in my functions

<?php

// Get all the products for the registration dropdown list

function get_products() {
    global $db;
    $query = 'SELECT * FROM products
              ORDER BY productCode';
    $statement = $db->prepare($query);
    $statement->execute();
    return $statement;    
}

function get_product_name($productName) {
    global $db;
    $query = 'SELECT * FROM products
              WHERE productCode = :product_code';    
    $statement = $db->prepare($query);
    $statement->bindValue(':product_name', $productName);
    $statement->execute();    
    $product = $statement->fetch();
    $statement->closeCursor();    
    $product_name = $product['name'];
    return $product_name;
}

function get_products_by_name($name) {
    global $db;
    $query = 'SELECT * FROM products
              WHERE products.name = :name
              ORDER BY productCode';
    $statement = $db->prepare($query);
    $statement->bindValue(":name", $name);
    $statement->execute();
    $products = $statement->fetchAll();
    $statement->closeCursor();
    return $products;
}

product_register.php

<?php include '../view/header.php'; ?>
<?php require('../model/database.php'); ?>
<main>

    <h2>Register Product</h2>
    <?php if (isset($message)) : ?>
        <p><?php echo $message; ?></p>
        <?php
    else:
        $email = filter_input(INPUT_POST, 'email');
        ?>

    <?php endif; ?>
         <form action="index.php" method="post">
    <label>Customer:</label><br>
<?php echo $email; ?>
    <label>Product:</label>
    <select>
        <?php foreach ( $categories as $category ) : ?>
            <option value="<?php echo $cateogry['productCode']; ?>">
                <?php echo $category['name']; ?>
            </option>
        <?php endforeach; ?>
        </select><br>

        <input type="hidden" name="action" value="register_product">
        <input type="submit" value="Register Product">
    </form>
</main>
<?php include '../view/footer.php'; ?>

What am I doing wrong?

tereško
  • 58,060
  • 25
  • 98
  • 150
HawkBlade124
  • 107
  • 1
  • 9
  • Show ```product_register.php``` content – Wolen Feb 26 '17 at 01:27
  • I'd look at line `19` in `product_register.php`. I expect there's a reference to a variable that isn't in scope. – spencer7593 Feb 26 '17 at 01:27
  • 1
    was this line `header("Location: product_register.php");` an include before? Right now you are redirecting, hence `$categories` "gets lost". – Jeff Feb 26 '17 at 01:35
  • in function get_product_name, you have `WHERE productCode = :product_code` that dont match the bindValue: `:product_name` you should change either to match your intentions – Michael Krikorev Feb 26 '17 at 01:35
  • @Jeff, it was an include before. its the stupid mistakes like this that break a project. thanks! – HawkBlade124 Feb 26 '17 at 01:38

3 Answers3

2

You're doing redirect from index.php to product_register.php where you didn't declared $categories variable. If you use header() to redirect user to another page and you need to pass some data I advise you to use session. You have to start session by using session_start() on the top of file. In this case you have to call this function in both files.

Then before header() in index.php just save $categories in session eg.

$_SESSION['categories'] = get_products();

Then in product_register.php instead

<?php foreach ( $categories as $category ) : ?>

do

<?php foreach ( $_SESSION['categories'] as $category ) : ?>

or

<?php
$categories = $_SESSION['categories'];
foreach ( $categories as $category ):
?>
Wolen
  • 874
  • 6
  • 15
2

Since you've changed an include to a redirect here:

header("Location: product_register.php");

the reference to $categories gets lost.

Either change it back to include('product_register.php') or go with @Wolen's solution.

Jeff
  • 6,895
  • 1
  • 15
  • 33
0

This is not the correct answer

I thought it was when I posted it However I am not going to delete, as I think it still shows the need to follow the flow of the code and understand it - I stopped at seeing the if statements so that OP could continue to debug - I didn't spot the header() statement inside one of them.

Although the variable is defined, it is nested inside 3 if/else statements:

if ($action == 'login') {
    if ($email == NULL || $email == FALSE) {
    } else {
        if ($custEmail) {
             $categories = get_products();

So either:

  • $action does not equal 'login'
  • or $email is NULL or FALSE
  • or $custEmail is empty, NULL, FALSE or a number of other non true statements.
Theo
  • 1,608
  • 1
  • 9
  • 16