我在这里泡菜。每当我尝试按下项目中的删除按钮时,它会直接进入PHP文件,对我为其设置的数据库不执行任何操作。
视觉编号2
这是index.php的代码:
<?php
require_once('database.php');
//Get 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
$queryAllCategories = 'SELECT * FROM categories
ORDER BY categoryID';
$statement2 = $db -> prepare($queryAllCategories);
$statement2->execute();
$categories = $statement2->fetchAll();
//Get products fpr 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>
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="../main1.css"/>
</head>
<body>
<header><h1>Product Manager</h1></header>
<main>
<hr>
<h1>Product List</h1>
<aside>
<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> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td><?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>
</tr>
<?php endforeach; ?>
</table>
<p><a href = "add_product_form.php">Add Product</a></p>
</section>
</main>
<hr>
<footer><p>$copy; <?php echo date("Y"); ?> My Guitar Shop Inc</p></footer>
</body>
</html>
delete_product.php的代码:
<?php
require_once('database.php');
$product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
//Delete the product from the datavase
if ($product_id != false && $category_id != false){
$query = 'DELETE FROM products
WHERE productID = :product_id';
$statement = $db -> prepare($query);
$statement -> bindValue(':product_id', $product_id);
$success = $statement->execute();
$statement -> closeCursor();
}
//Display the Product List Page
include('index.php');
?>
感谢您帮助我!我真的很感激!
答案 0 :(得分:2)
您正在post
方法中提交删除按钮。所以你必须在删除页面的post方法中接收这些值。
只需在INPUT_GET
INPUT_POST
更改为delete_product.php
即可
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
而不是
$product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
答案 1 :(得分:1)
你应该试试这个:
...
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
...