I am a student, this is week 5 learning php/mySQL. I've pretty much gathered that there's an undefined variable somewhere but I can't seem to find where. I have changed the variable names to productID product_id to no avail all over the document, and tried to echo the variable just in the html and still get the same error. I am getting this error:
<br /><b>Notice</b>: Undefined variable: product in <b>D:\xampp\htdocs\ch05_ex1\product_manager\product_edit.php</b> on line <b>10</b><br />
<br /><b>Notice</b>: Undefined variable: product in <b>D:\xampp\htdocs\ch05_ex1\product_manager\product_edit.php</b> on line <b>15</b><br />
<br /><b>Notice</b>: Undefined variable: product in <b>D:\xampp\htdocs\ch05_ex1\product_manager\product_edit.php</b> on line <b>20</b><br />
<br /><b>Notice</b>: Undefined variable: product in <b>D:\xampp\htdocs\ch05_ex1\product_manager\product_edit.php</b> on line <b>25</b><br />
When I try to update a form this is supposed to populate with the product information
This is in my index
<?php
require('../model/database.php');
require('../model/product_db.php');
require('../model/category_db.php');
$action = filter_input(INPUT_POST, 'action');
if ($action == NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action == NULL) {
$action = 'list_products';
}
}
//ACTION TO LIST PRODUCTS
if ($action == 'list_products') {
$category_id = filter_input(INPUT_GET, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE) {
$category_id = 1;
}
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
include('product_list.php');
//ACTION TO EDIT PRODUCTS
} if ($action == 'edit_product') {
$category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$price = filter_input(INPUT_POST, 'price');
if ($category_id == NULL || $category_id == FALSE ||
empty($code)|| empty($name) ||
$price == NULL || $price == FALSE) {
$error = "Invalid product data. Check all fields and try again.";
include('../errors/error.php');
} else {
edit_product($category_id, $code, $name, $price);
header("Location: .?category_id=$category_id");
}
This is in my product_list these are my buttons
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td class="right"><?php echo $product['listPrice']; ?></td>
<td><form action ="product_edit.php" method="post"
value="category_id">
<input type="hidden" name="product_id"
value="edit_product">
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>">
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>">
<input type="submit" value="Edit">
</form></td>
This is my function in my product_db
function edit_product($category_id, $code, $name, $price) {
global $db;
$query = 'UPDATE products
SET categoryID = :category_id,
productCode = :code,
productName = :name,
listPrice = :price
WHERE productID = :product_id';
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id);
$statement->bindValue(':code', $code);
$statement->bindValue(':name', $name);
$statement->bindValue(':price', $price);
$statement->bindValue(':product_id', $product_id);
$statement->execute();
$statement->closeCursor();
}
?>
And this is my product_edit form
<?php include '../view/header.php'; ?>
<main>
<h1>Edit Product</h1>
<form action="index.php" method="post" id="add_product_form">
<input type="hidden" name="action"
value="<?php echo $product['productID']; ?>">
<label>Category ID:</label>
<input type="input" name="category_id"
value="<?php echo $product['categoryID']; ?>">
<br>
<label>Code:</label>
<input type="input" name="code"
value="<?php echo $product['productCode']; ?>">
<br>
<label>Name:</label>
<input type="input" name="name"
value="<?php echo $product['productName']; ?>">
<br>
<label>List Price:</label>
<input type="input" name="price"
value="<?php echo $product['listPrice']; ?>">
<br>
<label> </label>
<input type="submit" value="Save Changes"><br>
<br>
</form>
<p class="last_paragraph">
<a href="index.php?action=list_products">View Product List</a>
</p>
</main>
<?php include '../view/footer.php'; ?>