douyi2664 2016-11-26 06:58
浏览 181

如何使用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 -->
  • 写回答

3条回答 默认 最新

  • douyi0219 2016-11-26 07:15
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大