如何使用PHP

时间:2016-11-26 06:58:38

标签: php mysql sql

我正在为一所学校做项目。我目前有一个产品页面来显示各种各样的项目,包括图像,描述和价格等......

在每个产品下我都有一个删除按钮,当以管理员身份登录时,显示正常。

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

我想知道如何在单击删除按钮时从MySQL表中删除数据行。

<?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 个答案:

答案 0 :(得分:0)

一种方法

button更改为a元素并使href看起来像这样:

yourdomain.tld/products/delete/{id}

您必须在id位置回显mysql数据库中的主键。它看起来像这样:

yourdomain.tld/products/delete/5

然后,您必须更改.htaccess,以便所有请求都转到根项目中的index.php。在index.php,您可以进行实际查询。

更新

请注意,访问此网址的任何人都可以使用此方法删除产品。您必须确保只有管理员才能这样做。首选方法是POST请求。

您还可以将主键参数发送到刚刚显示的PHP脚本。使用这种方法,您无需编辑.htaccess。您可以将其作为URL参数传递,如下所示:

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

在您的脚本中,您可以获得如下参数:

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

答案 1 :(得分:0)

如果要从数据库中删除记录的整行,可以这样做。这样您就可以传递产品ID并删除该行。只需使用绑定参数概念

将id与查询绑定
$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);
}

答案 2 :(得分:0)

您应该发布到帖子数据中带有ID的网址,然后重定向回原来的位置。

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