简体   繁体   English

使用复选框从一个表中删除行数据并将删除的行数据插入另一个表中

[英]Delete row data from one table using check box and insert the deleted row data in another table

/*I have two table , 1st table name is bazar and 2nd table name is bazarduepayment having same columne name : sl,date,item,paid,due,remark. /*我有两个表,第一个表名是bazar,第二个表名是bazarduepayment,列名相同:sl,date,item,paid,due,remark。 'sl' is auto increment . 'sl' 是自动增量。 Delete function is working perfectly .删除功能运行良好。 Someone please help me how to insert deleted row data in 2nd table 'bazarduepayment' Here below is code detail i wrote */有人请帮助我如何在第二个表“bazarduepayment”中插入已删除的行数据下面是我写的代码详细信息*/

<?php
session_start();
include_once("rwdbconnection.php");
error_reporting(0);

if(isset($_POST['save']))
 {
   $checkbox = $_POST['check'];
   for($i=0;$i<count($checkbox);$i++)
    {
    $del_id = $checkbox[$i]; 
    mysqli_query($conn,"DELETE FROM bazar WHERE sl='".$del_id."'");
    $message = "Data deleted successfully !";

   }
   }
    $result = mysqli_query($conn,"SELECT * FROM bazar");
    ?>

<!DOCTYPE html>
<html>
<head>
        <linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Delete data</title>
 </head>
  <body>

   <div>
   <?php if(isset($message)) { echo $message; } ?>
   </div>

   <form method="post" action="">
   <table class="table table-bordered">
   <thead>
   <tr>
   <th><input type="checkbox" id="checkAl"> Select All</th>
   <th>Sl</th>
   <th>Date</th>
   <th>Item</th>
   <th>Paid</th>
   <th>Due</th>
   <th>Remark</th>
   </tr>
   </thead>
   <?php
        $i=0;
        while($row = mysqli_fetch_array($result)) 
        {
        ?>
        <tr>
        <td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["sl"]; ?>"></td>
        <td><?php echo $row["sl"]; ?></td>
        <td><?php echo $row["date"]; ?></td>
        <td><?php echo $row["item"]; ?></td>
        <td><?php echo $row["paid"]; ?></td>
        <td><?php echo $row["due"]; ?></td>
        <td><?php echo $row["remark"]; ?></td>
        </tr>
        <?php
        $i++;
        }
        ?>
        </table>
        <p align="center"><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
        </form>
        <script>
        $("#checkAl").click(function () {
        $('input:checkbox').not(this).prop('checked', this.checked);
        });
        </script>
        </body>
        </html> 

First you need to copy the data from one table to another using INSERT ... SELECT syntax and only then you can delete.首先,您需要使用INSERT ... SELECT语法将数据从一个表复制到另一个表,然后才能删除。

You should be using prepared statements for this.您应该为此使用准备好的语句。

if (isset($_POST['save'])) {
    // Prepared INSERT query
    $stmt_insert = $conn->prepare('INSERT INTO bazarduepayment(date,item,paid,due,remark) 
        SELECT date,item,paid,due,remark FROM bazar WHERE sl=?');

    // Prepare DELETE query
    $stmt_delete = $conn->prepare('DELETE FROM bazar WHERE sl=?');

    // Loop on all checkboxes selected
    foreach ($_POST['check'] as $del_id) {
        $stmt_insert->bind_param('s', $del_id);
        $stmt_insert->execute();

        $stmt_delete->bind_param('s', $del_id);
        $stmt_delete->execute();
    }
}

You could even simplify this to get rid of the foreach loop entirely.您甚至可以简化它以完全摆脱foreach循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySql,将数据插入另一个表行的表行(此行刚刚创建) - MySql, Insert data to a table row from another table row (this row is just been created) 通过选择一行将数据从另一个表发送到一个表 - Send data to a table from another table with choosing one row 在laravel中,如何从一个表中检索链接到另一表上已删除行的数据时排除行 - In laravel, how to exclude rows while retrieving data from one table which have link to a deleted row on another table 如何使用复选框将表中的多行数据插入 MySQL 中的另一个表 - How to insert multiple row data from a table into another table in MySQL using checkboxes 如何从一个表中删除一个条目,然后将删除的数据插入另一个表? (PHP,MySQL) - How to delete an entry FROM a table and then INSERT the deleted data INTO another table? (PHP, MySQL) 如果laravel中的表数据行已删除,如何删除文件? - How to delete file if the row of table data had deleted in laravel? Laravel 使用按钮将行数据从表移动到另一个表 - Laravel move row data from table to another table using button 如果在另一行中删除了一行,如何更新表 - How to update a table if a row is deleted in another one 使用Symfony将数据从一个表插入到另一个表 - Insert data from one table to another table using Symfony 如果选中了复选框,则将多行插入表格 - Multiple row insert to table if check box is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM