PHP下拉列表之前正在运行,现在却没有

时间:2017-02-26 01:22:14

标签: php mysql

所以我对于让这个下拉菜单工作需要做些什么并不是最模糊的想法。之前它正在工作,但是当我做了一些修改时,它已经无法正常工作了。显示的错误消息是:

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

但我在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');
            }
        }
    }
?>

我在我的函数中定义了它

<?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'; ?>

我做错了什么?

3 个答案:

答案 0 :(得分:2)

您正在从index.php重定向到product_register.php,而您没有声明$categories变量。如果您使用header()将用户重定向到另一个页面,并且您需要传递一些数据,我建议您使用会话。您必须使用文件顶部的session_start()启动会话。在这种情况下,您必须在两个文件中调用此函数。

然后在 index.php 中的header()之前,只需在会话中保存$categories,例如

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

然后在 product_register.php 中 代替

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

DO

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

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

答案 1 :(得分:2)

由于您已在此处将包含更改为重定向:

header("Location: product_register.php");

$categories的引用会丢失。

将其更改回include('product_register.php')或使用@ Wolen的解决方案。

答案 2 :(得分:0)

这不是正确的答案

我以为是我发布的时候 但是我不会删除,因为我认为它仍然显示需要遵循代码流并理解它 - 我停止看到if语句以便OP可以继续调试 - 我没有发现其中一个内有header()声明。

虽然定义了变量,但它嵌套在3 if / else语句中:

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

所以:

  • $action不等于'login'
  • $emailNULLFALSE
  • $custEmail为空,NULLFALSE或其他一些非真实陈述。