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 and try again.";
include('error.php');
} else {
require_once('database.php');
// edit the product to the database
$query = "UPDATE products
SET categoryID = :category_id,
productCode = :code,
productName = :name,
listPrice = :price
WHERE productID =:product_id";
$stmt = $db->prepare($query);
$stmt->bindParam(':category_id', $category_id);
$stmt->bindParam(':code', $code);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':price', $price);
$stmt->bindParam(':product_id', $product_id);
$stmt->execute();
// Display the Product List page
include('index.php');
}
?>
edit_form.php
<?php
$product_id = $_POST['product_id'];
require_once('database.php');
//getting categories
$query = "SELECT *
FROM categories
ORDER BY CategoryID";
$categories = $db->query($query);
//geting product information
$query = "SELECT *
FROM products
WHERE productID = $product_id";
$edit_product = $db->query($query);
$edit_product = $edit_product->fetch();
$code = $edit_product['productCode'];
$name = $edit_product['productName'];
$price = $edit_product['listPrice'];
$category_id = $edit_product['categoryID'];
?>
<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>
<form action="edit_product.php" method="post"
id="add_product_form">
<input type="hidden" name="productID" value="<?php echo $product_id; ?>">
<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="text" name="code" value = "<?php echo $code; ?>"> <br>
<label>Name:</label>
<input type="text" name="name" value = "<?php echo $name; ?>"><br>
<label>List Price:</label>
<input type="text" name="price" value = "<?php echo $price; ?>"><br>
<label> </label>
<input type="submit" value="Edit 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>
index.php
<?php
require_once('database.php');
// Get category ID
if (!isset($category_id)) {
$category_id = filter_input(INPUT_GET, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE) {
$category_id = 1;
}
}
// Get name for selected category
$queryCategory = 'SELECT * FROM categories
WHERE categoryID = :category_id';
$statement1 = $db->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll();
$statement->closeCursor();
// Get products for selected category
$queryProducts = 'SELECT * FROM products
WHERE categoryID = :category_id
ORDER BY productID';
$statement3 = $db->prepare($queryProducts);
$statement3->bindValue(':category_id', $category_id);
$statement3->execute();
$products = $statement3->fetchAll();
$statement3->closeCursor();
?>
<!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>Product List</h1>
<aside>
<!-- display a list of categories -->
<h2>Categories</h2>
<nav>
<ul>
<?php foreach ($categories as $category) : ?>
<li><a href=".?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<section>
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?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="delete_product.php" method="post">
<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="Delete">
</form></td>
<td><form action="edit_product_form.php" method="post" id="edit_product_form">
<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>
</tr>
<?php endforeach; ?>
</table>
<p><a href="add_product_form.php">Add Product</a></p>
<p><a href="category_list.php">List Categories</a></p>
</section>
</main>
<footer>
<p>© <?php echo date("Y"); ?> My Guitar Shop, Inc.</p>
</footer>
</body>
</html>