EDIT_PRODUCT.PHP php // Get the product data $category_id = $_POST['category_id']; $code = $_POST['code']; $name = $_POST['name']; $price = $_POST['price']; $product_id = $_POST['productID'];
My update button does not work, when i press edit to update data. i get an unidentified index. Plz help!
EDIT_PRODUCT.PHP
<?php
// Get the product data
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
$product_id = $_POST['productID'];
// Validate inputs
if (empty($code) || empty($name) || empty($price)) {
$error = "Invalid product data. Check all fields are try again.";
include('error.php');
} else {
// If valid, add the product to the
require_once('database.php');
$query = "UPDATE products
SET categoryID = $category_id,
productCode = $code,
productName = $name,
listPrice = $price
WHERE productID = $product_id";
$db->exec($query);
// Display the Product List page
include('index.php');
}
EDIT_PRODUCT_FORM.PHP
<?php
// get post values
$product_id = $_POST['product_id'];
// This is the drop down menu next to 'Category:'
require('database.php');
$query = 'SELECT *
FROM categories
ORDER BY categoryID';
$categories = $db->query($query);
$query = "SELECT *
FROM products
WHERE productID = $product_id";
$edit_product = $db->query($query);
$edit_product = $edit_product->fetch();
// Define the values
$code = $edit_product['productCode'];
$name = $edit_product['productName'];
$price = $edit_product['listPrice'];
$category_id = $edit_product['categoryID'];
?>
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" href="main.css">
</head>
<!-- the body section -->
<body>
<header>
<h1>Product Manager</h1>
</header>
<main>
<h1>Edit Product</h1>
category id: <?php echo $category_id; ?> <br>
<form action="edit_product.php" method="post" id="edit_product_form">
<label>Category:</label>
<select name="category_id">
<?php foreach ($categories as $category) : ?>
<option value="<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</option>
<?php endforeach; ?>
</select><br>
<label>Code:</label>
<input type="input" name="code" value="<?php echo $code; ?>"><br>
<label>Name:</label>
<input type="input" name="name" value="<?php echo $name; ?>"><br>
<label>List Price:</label>
<input type="input" name="price" value="<?php echo $price; ?>"> <br>
<label> </label>
<input type="submit" value="Update Product"><br>
</form>
<p><a href="index.php">View Product List</a></p>
</main>
<footer>
<p>© <?php echo date("Y"); ?> My Guitar Shop, Inc.</p>
</footer>
</body>
</html>
When working with PHP, you come across two methods: $_POST and $_GET. These methods are used to get values from the user through the form. Using them may result in a "Notice: Undefined Index" error. This error means that your code has a variable or constant that has not been assigned a value. However, there are times when you want to use values obtained through a user form in your PHP code. This error can be avoided by using the isset() function. This function checks if the index variable has been assigned a value before using it
Step by step
Solved in 2 steps