douhuangjian9627 2015-04-10 05:37
浏览 56
已采纳

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

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,

Now i want to perform when someone delete any row then it moves that row to another table i.e. into "trash"

Delete QUERY is perfectly Working so i want to modify this code... Here's my Code -

<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

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

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条回答 默认 最新

  • douzao9845 2015-04-10 06:30
    关注

    In the delete.php you should write something like below

    <?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
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?