所以我不知道需要做什麼才能讓下拉菜單工作。它在工作之前,但是當我做了一些修改後,它不再工作了。顯示錯誤消息是:PHP下拉式工作之前,現在不是
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'; ?>
我是什麼doi恩錯了嗎?
顯示'''product_register.php'''內容 – Wolen
我會看'product_register.php'中的行'19'。我希望有一個參考範圍內的變量。 – spencer7593
是這一行'header(「Location:product_register.php」);'之前的包含?現在你正在重定向,因此「$ categories」會「丟失」。 – Jeff