[英]Delete ROWS from one table and then INSERT into another TABLE in MYSQLI
I have two table names called as "message" and "trash" 我有两个表名,分别称为“消息”和“垃圾箱”
In "message" table, i've some data in table format contains (TO, SUBJECT, MSG, DATE) and i applied CHECKALL functionality with CHECKBOX using Jquery, 在“邮件”表中,我有一些表格式的数据包含(TO,SUBJECT,MSG,DATE),并且我使用Jquery在CHECKBOX中应用了CHECKALL功能,
Now i want to perform when someone delete any row then it moves that row to another table ie into "trash" 现在,当某人删除任何行时,我想执行该操作,然后将该行移至另一张表,即“垃圾箱”中
Delete QUERY is perfectly Working so i want to modify this code... Here's my Code - 删除QUERY完全正常,因此我想修改此代码...这是我的代码-
<form action="delete.php" method="post" name="data_table">
<table class="table table-hover table-striped">
<tbody>
<tr style="background-color:#222D32;">
<td><input type="checkbox" id="check_all" value=""></td>
<td><input name="submit" type="submit" value="Delete" id="submit" class="btn btn-default btn-sm"></td>
<td></td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" value="<?php echo $rw['id']; ?>" name="data[]" id="data"></td>
<td><b><?php echo $rw['fto']; ?></td>
<td><b><?php echo $rw['subject']; ?></b></td>
<td><b><?php echo $rw['date']; ?></b></td>
<tr>
</tbody>
</table>
</form>
And here's the delete.php file 这是delete.php文件
<?php
require_once("config.php");
if(isset($_POST['submit'])) {
$id_array = $_POST['data']; // return array
$id_count = count($_POST['data']); // count array
for($i=0; $i < $id_count; $i++) {
$id = $id_array[$i];
$query = mysqli_query($con, "DELETE FROM `message` WHERE `id` = '$id'");
if(!$query) { die(mysqli_error()); }
}
header("Location: sent.php"); // redirect after deleting
}
?>
Here's the JS File which i included at top 这是我顶部包含的JS文件
jQuery(function($) {
$("form input[id='check_all']").click(function() { // triggred check
var inputs = $("form input[type='checkbox']"); // get the checkbox
for(var i = 0; i < inputs.length; i++) { // count input tag in the form
var type = inputs[i].getAttribute("type"); // get the type attribute
if(type == "checkbox") {
if(this.checked) {
inputs[i].checked = true; // checked
} else {
inputs[i].checked = false; // unchecked
}
}
}
});
$("form input[id='submit']").click(function() { // triggred submit
var count_checked = $("[name='data[]']:checked").length; // count the checked
if(count_checked == 0) {
alert("Please select a row(s) to delete.");
return false;
}
if(count_checked == 1) {
return confirm("Are you sure you want to delete?");
} else {
return confirm("Are you sure you want to delete?");
}
});
});
In the delete.php you should write something like below 在delete.php中,您应该编写如下内容
<?php
require_once("config.php");
if(isset($_POST['submit'])) {
$id_array = $_POST['data']; // return array
$id_count = count($_POST['data']); // count array
for($i=0; $i < $id_count; $i++) {
$id = $id_array[$i];
$query = mysqli_query($con, "INSERT INTO trash SELECT * FROM `message` WHERE `id` = '$id'");
$query = mysqli_query($con, "DELETE FROM `message` WHERE `id` = '$id'");
if(!$query) { die(mysqli_error()); }
}
header("Location: sent.php"); // redirect after deleting
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.