[英]PHP How to Delete Multiple Records using Checkbox
關於刪除表中的多個記錄,我有一個小問題。 我使用復選框來刪除它們,但是它不起作用。 我不知道確切的代碼是什么。
這是我的PHP代碼
<?php
echo "<form action='#'>
<fieldset>
<input type='text' name='search' value='' id='searchtalents' placeholder='Search Keywords..' size='40'/>
</fieldset>
</form>";
echo "<form name='tmsform' method='POST' action=''>";
$sql = "SELECT * FROM talentinfo WHERE 1 LIMIT 10";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found";
exit;
}
echo"<div id='talentinfo'><table id='mr_database' name='myform' style='border:1px solid #fff;' cellspacing=0 cellpading='2' class='pretty'>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th>Mr Tracking Number</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Contact Number</th>
<th>School</th>
<th>Course</th>
<th>Year Graduated</th>
<th>Position Considered</th>
<th>Referred Location</th>
<th>Unit</th>
</tr>
</thead>";
$counter = 40000;
while ($row = mysql_fetch_assoc($result)) {
$filled = (($counter % 2) == 1) ? "style='background-color:#BCD9E1;'" : "" ;
$id = $row['talents_id'];
echo "<tbody><tr {$filled} class='tmsdel'>";
echo "<td><a href ='#' rel='#edit_talents{$counter}'><center><img src='img/edit.gif' width='25' height='21' title='Edit'></center></a></td>";
echo "<td><a href ='#' id=".$row['talents_id'].'&idelete=talents'." class='delete'><center><img src='img/delete.png' width='25' height='21' title='Delete'></center></a></td>";
echo "<td><input type='checkbox' name='checkbox[]' id='check".$row['talents_id']."' value='".$row['talents_id'].'&idelete=talents'."'/></td>";
echo "<td><a href='#' rel='#tracing_number{$counter}' style='text-decoration:none; font-weight:bold; color:#444;'>" . $row ['mr_tracking_number'] . "</a></td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact_number'] . "</td>";
echo "<td>" . $row['school'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['year_graduated'] . "</td>";
echo "<td>" . $row['position_considered'] . "</td>";
echo "<td>" . $row['referred_location'] . "</td>";
echo "<td>" . $row['unit'] . "</td>";
echo "</tr></tbody>";
?>
這是我的Javascript
$(function(){
$(document).ready(function(){
});
$("#delete-all").click(function(){
$("#mr_database").remove();
$.ajax({
url: "modules/delete-all.php",
type: "get",
async: false,
data: "check"
});
});
});
請按照以下步驟操作:
單擊按鈕上的呼叫功能,然后單擊刪除
var allVals = ''; $("#delete-all").click(function(){ $('.checkboxclass :checked').each(function() { allVals = allVals + $(this).val() + ','; }); }
然后,您需要在ajax
傳遞allVals
變量並將其發布到.php文件
像:data: 'ids=' + allVals
$ .ajax中的'ids=' + allVals
最后,您可以在php文件中獲取此變量,並對其執行刪除過程。
像這樣: $ids = explode(',', $_REQUEST['ids');
並在mysql查詢中使用id
希望這對您有所幫助。
答案很簡單,因為我在這里看到的是一個腳本,用於查看表並刪除瀏覽器中的html,而不是服務器上的數據。
您需要編寫此處提到的modules / delete-all.php腳本:url:“ modules / delete-all.php”
其中應包含“ delete * FROM人才信息” SQL查詢。
但是,這將無縫刪除所有表內容。 因此,您可能需要進行提示:
$("#delete-all").click(function(){
Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)");
if (Check == true) {
$("#mr_database").remove();
$.ajax({
url: "modules/delete-all.php",
type: "get",
async: false,
data: "check"
});
}
});
delete_all.php應該刪除所有內容,因此只需執行以下操作:
<?php
$sql = "delete * FROM talentinfo ";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
exit;
} else {
print "ok";
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.