繁体   English   中英

从一个表中删除ROWS,然后在MYSQLI中插入到另一个表中

[英]Delete ROWS from one table and then INSERT into another TABLE in MYSQLI

我有两个表名,分别称为“消息”和“垃圾箱”

在“邮件”表中,我有一些表格式的数据包含(TO,SUBJECT,MSG,DATE),并且我使用Jquery在CHECKBOX中应用了CHECKALL功能,

现在,当某人删除任何行时,我想执行该操作,然后将该行移至另一张表,即“垃圾箱”中

删除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>

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

这是我顶部包含的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?");
          }
    });
});

在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM