简体   繁体   中英

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. 'sl' is auto increment . 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 */

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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