dtip91401 2017-01-29 00:52
浏览 8
已采纳

无法使用PHP从表中删除产品

I am in a pickle here. Whenever I try to press the delete button on my project, it goes straight to the PHP file and does nothing to the database that I have set up for it.

Here are some visuals to help you: When I press delete

Visual number 2

enter image description here

Here is the code for 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>&nbsp;</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>

code for 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');

?>

Thank you for helping me out! I really appreciate it!

  • 写回答

2条回答 默认 最新

  • doulan1866 2017-01-29 00:58
    关注

    Your are submitting delete button in post method. So you have to receive these value in post method in delete page. Just change INPUT_GET to INPUT_POST in two lines in 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);
    

    instead of

    $product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
    $category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样