2017-01-29 7 views
0

私はここにピクルスにいます。プロジェクトの削除ボタンを押すたびに、PHPファイルに直接移動し、データベースには何も設定しません。PHPを使用してテーブルから商品を削除できません

はここであなたを助けるために、いくつかのビジュアルです: When I press delete

ビジュアル数2

ここ

enter image description here

は、index.phpのためのコードです:delete_productため

<?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> 

コード。 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'); 

?> 

私を助けてくれてありがとう!ほんとうにありがとう!

+2

あなたが混乱し – nogad

+0

おかげで男をGETとPOST持ちで二列にINPUT_POSTINPUT_GETを変更します。通常、物事を把握するために目の第二のセットを持つのに役立ちます。 :) – HawkBlade124

答えて

2

あなたがpost方法で削除ボタンを提出しています。だからあなたは削除ページで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

あなたはこれを試してみてください:

... 
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT); 
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT); 
...