如何使用PHP中的html delete按钮从MySQL表数据中删除一行

I am working on a project, for school. I currently have a product page to display an assortment of item includes image, description and price etc...

Under each product I have a delete button, when logged in as admin, which displays fine.

     if (is_admin())
        echo '<a href ="#"><button>Delete item</button></a>'; }

I want to know how remove the row of data from MySQL table on clicking the delete button.

<?php
        // Include need php scripts
        require_once ("Includes/simplecms-config.php");
        require_once ("Includes/connectDB.php");
        include ("Includes/header.php");

        if (!empty($_GET['cat'])) {
            $category = $_GET['cat'];
            $query = mysqli_query($db, "SELECT * FROM products WHERE category = '".$category."'");
        } else {
            $query = mysqli_query($db, "SELECT * FROM products");
        }

        if (!$query) {
            die('Database query failed: ' . $query->error);
        } 

       $deleted = mysql_query($db, "DELETE FROM products");  
    ?>

    <section>
        <div id="productList">
            <?php
                $row_count = mysqli_num_rows($query);
                if ($row_count == 0) {
                    echo '<p style="color:red">There are no images uploaded for this category</p>';
                } elseif ($query) {
                    while($products = mysqli_fetch_array($query)){             
                        $file = $products['image'];
                        $product_name = $products['product'$];
                        $image_id = $products['id'];
                        $price = $products['price'];
                        $desc = $products['description'];
                        echo '<div class="image_container">';
                        echo '<a href="viewProduct.php?id=' . $image_id . '"><p><img src="Images/products/'.$file.'" alt="'.$product_name.'" height="250" /></p>';
                        echo '' . $product_name ."</a><br>$" . $price . "<br>" . $desc;


                        echo '</div>';

                            if (is_admin()){
                           echo '<a href ="#"><button>Delete item</button></a>'; 
                          }

                     } 
                 } else {
                     die('There was a problem with the query: ' .$query->error);             
                 } 
                 mysqli_free_result($query);     
            ?>
        </div>
    </section>
    <?php include ("Includes/footer.php"); ?>



<!-- end snippet -->

One approach

Change the button to a a element and make the href look like this:

yourdomain.tld/products/delete/{id}

You have to echo the primary key from your mysql database at the id position. It will look like this:

yourdomain.tld/products/delete/5

Then you have to change your .htaccess in a way that all requests go to your index.php in your root project. At the index.php you can do the actually query then.


Update

Keep in mind that anyone visiting this URL can delete products with this approach. You have to make sure that only the admin can do that. The preferred method is a POST request.

You can also send the primary key parameter to your PHP script you are just showed. With this approach you don't need to edit your .htaccess. You may pass it as an URL parameter like this:

yourdomain.tld/your-script.php?delete-product={id}

In your script you can get the parameter like this:

<?php
    if (isset($_GET['delete-product'])) {
    // your mysql query to delete the product
} else {
   // something else
}

If you want to delete the entire row of an record from your db you can do like this. So that you can pass the product id and delete the row. Just bind the id with query using bind parameters concept

$knownStmt=mysqli_prepare($conn, "DELETE FROM  `YourTableName` WHERE `pdt_id` = ?;");
if( $knownStmt ) {
    mysqli_stmt_bind_param($knownStmt,"d",$pdt_id);
    mysqli_stmt_execute($knownStmt); 
    mysqli_stmt_close($knownStmt);
}

You should post to a url with the id in the post data, then redirect back to where you were.

<?php
//html on productpage
if(isset($_GET['product_deleted'])){
    if($_GET['product_deleted'] === 'true'){
        echo 'The product was deleted';
    }else{
        echo 'The product could not be deleted';
    }
}
if (is_admin()){
    /**
     * It's a good idea for the page that deletes to be different from the one your on, so that when you redirect back, 
     * they can refresh the page without getting something 
     * along the lines of 'refreshing with page will re-post the data'
     */
    ?>
    <form method="POST" action="/product/delete.php">
        <button>Delete item</button>
        <input type="hidden" name="id" value="<?php echo $image_id; ?>" />
    </form>
    <?php
}

//PHP on /product/delete.php
if(is_admin() && $_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['id'])){
    //delete sql here
    header('Location: /productpage.php?product_deleted=true'); //redirect back
}