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

时间:2015-04-10 05:37:50

标签: php jquery mysqli

我有两个表名称为“message”和“trash”

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

现在我想执行当有人删除任何行然后它将该行移动到另一个表,即进入“垃圾”

删除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?");
          }
    });
});

1 个答案:

答案 0 :(得分:0)

在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
}
?>