我試圖刪除一些記錄,形成我的數據庫沿着它服從,使其出現在表中的規則,多個記錄,所以我試圖使用PHP和AJAX來完成這個動作。雖然我在線複製代碼以執行操作。 但是,這是我注意到,每當我選擇,我要刪除的記錄,它只會淡出選定行,但瀏覽網頁的記錄將會重新出現,這意味着我失去了一些東西,我不知道刷新,這裏是代碼\使用複選框以刪除使用PHP和AJAX
的index.php
<?php
//include auth.php file on all secure pages
require("../db.php");
session_start();
if(!isset($_SESSION["username"])){
header("Location: login");
exit(); }
?>
<?php require_once('header.php')?>
<?php
//index.php
$query = "SELECT * FROM consignment WHERE shipmentstatus='Pending'";
$result = mysqli_query($con, $query);
?>
<div class="container content">
<?php
if(mysqli_num_rows($result) > 0)
{
?>
<div class="table-responsive">
<table id="myTable" class="table table-striped" >
<thead>
<tr>
<th>Consignment No</th>
<th>Origin</th>
<th>Destination</th>
<th>Pickup Date</th>
<th>Status</th>
<th >Action</th>
</tr>
</thead>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tbody>
<tr id="<?php echo $row["consignmentno"]; ?>" >
<td><?php echo $row["consignmentno"]; ?></td>
<td><?php echo $row["shipmentorigin"]; ?></td>
<td><?php echo $row["shipmentdestination"]; ?></td>
<td><?php echo $row["shipmentpickupdate"]; ?></td>
<td><?php echo $row["shipmentstatus"]; ?></td>
<td><input type="checkbox" name="customer_id[]" class="delete_customer" value="<?php echo $row["consignmentno"]; ?>" /></td>
</tr>
</tbody>
<?php
}
?>
</table>
</div>
<?php
}
?>
<div align="center">
<button type="button" name="btn_delete" id="btn_delete" class="btn btn-success">Delete</button>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#btn_delete').click(function(){
if(confirm("Are you sure you want to delete this?"))
{
var id = [];
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(id.length === 0) //tell you if the array is empty
{
alert("Please Select atleast one checkbox");
}
else
{
$.ajax({
url:'deleterecord.php',
method:'POST',
data:{id:id},
success:function()
{
for(var i=0; i<id.length; i++)
{
$('tr#'+id[i]+'').css('background-color', '#ccc');
$('tr#'+id[i]+'').fadeOut('slow');
}
}
});
}
}
else
{
return false;
}
});
});
</script>
<?php require_once('footer.php')?>
這裏是刪除頁面處理器 deleterecord.php
<?php
require("../db.php");
session_start();
if(!isset($_SESSION["username"])){
header("Location: login");
exit(); }
?>
<?php
if(isset($_POST["id"]))
{
foreach($_POST["id"] as $id)
{
$query = "DELETE FROM consignment WHERE consignmentno = '".$id."'";
mysqli_query($con, $query);
}
}
?>
設置錯誤日誌記錄趕上警告,並檢查你得到什麼錯誤。它可能是你的數據庫連接,或者你的查詢。 –
您已開放給SQL注入,參數化該查詢。您還可以使用1個查詢執行所有刪除操作。 – chris85
嘗試檢查數組id中正在發送的值。您可以通過瀏覽器的控制檯執行此操作。 –