[英]How to remove a row from MySQL table data using html delete button in PHP
我正在為學校設計一個項目。 我目前有一個產品頁面來顯示各種商品,包括圖片,描述和價格等。
在每個產品下,當以admin身份登錄時,我都有一個刪除按鈕,該按鈕顯示正常。
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 -->
將button
更改為a
元素,並使href
如下所示:
yourdomain.tld/products/delete/{id}
您必須在id位置回顯mysql數據庫的主鍵。 它看起來像這樣:
yourdomain.tld/products/delete/5
然后,您必須更改.htaccess
,以使所有請求都進入根項目中的index.php
。 然后,您可以在index.php上進行實際查詢。
請記住,訪問此URL的任何人都可以使用此方法刪除產品。 您必須確保只有管理員才能做到這一點。 首選方法是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
}
如果要從數據庫中刪除記錄的整個行,可以這樣做。 這樣您就可以傳遞產品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);
}
您應該發布到包含發布數據中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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.