How to insert multiple row data from a table into another table in MySQL using checkboxes

I trying to insert my data from table_request into table_list by clicking the approve/reject button under the table.

Users will click the checkboxes to select all or select a specific row. After approve, the data in the table_request insert into table_list and deleted from table_request.

The current problem is about the foreach and inserts statement is wrong. After I click approve, it can only insert table_request id into table_list.

This is my table_request.php

<form name="bulk_action_form" action="action.php" method="post" onSubmit="return approve_confirm();"/>
<table class="bordered">
<tr>
  <th><input type="checkbox" name="select_all" id="select_all" value=""/></th>        
  <th>Name</th>
  <th>Remark</th>
</tr>

<?php
  $query = "select * from `table_request`;";
  if(count(fetchAll($query))>0){
  foreach(fetchAll($query) as $row){
?>

    <tr>
    <td><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['id']; ?>"/></td>
    <td><?php echo $row['Name'] ?></td>
    <td><?php echo $row['Remark'] ?></td>
    </tr>
    <?php } }else{ ?>
        <tr><td colspan="5">No records found.</td></tr> 
    <?php } ?>

</table>
<input type="submit" name="approve_btn" value="Approve"/>
</form>

This is my action.php

<?php
session_start();
include_once('connection.php');
if(isset($_POST['approve_btn']))
{
    $idArr = $_POST['checked_id'];
    $Name  = $_POST['Name'];
    $Remark = $_POST['Remark'];
    foreach($idArr as $key => $value)
    {
        $save = "INSERT INTO table_list(id,Name,Remark) VALUES ('".$value."','".$Name[$key]."','".$Remark[$key]."')";
                $query = mysqli_query($conn,$save);
    }
    $query .= "DELETE FROM `table_request` WHERE `table_request`.`id` = '$id';";
    header("Location:table_request.php");
}
?>

Answer

connection.php file

<?php
$server_name = "localhost";
$user_name   = "username";
$password    = "password";
$db_name     = 'database';

// Create connection
$conn = mysqli_connect($server_name, $user_name, $password, $db_name);

// Check connection
if (mysqli_connect_errno()) {
   die("Connection failed: ");
}

table_request.php file

<?php include_once('connection.php'); ?>

<form name="bulk_action_form" action="action.php" method="post" onSubmit="return approve_confirm();"/>
<table class="bordered">
  <tr>
    <th>Select</th>        
    <th>Name</th>
    <th>Remark</th>
</tr>
<?php
   $query = mysqli_query($conn, "SELECT * FROM table_request");
   if (mysqli_num_rows($query) > 0) : foreach($query as $row) : ?>
     <tr>
       <td><input type="checkbox" name="checked_id[]" class="checkbox" value=" 
          <?php echo $row['id']; ?>"/></td>
          <td><?php echo $row['Name']; ?></td>
          <td><?php echo $row['Remark']; ?></td>
     </tr>
   <?php endforeach; else : ?>
      <tr><td colspan="5">No records found.</td></tr> 
   <?php endif; ?>
</table>
  <input type="submit" name="approve_btn" value="Approve"/>
</form>

action.php file

<?php
    include_once('connection.php');

    if(isset($_POST['approve_btn'])) {
       foreach ($_POST['checked_id'] as $id) {
          $query = mysqli_query($conn, "SELECT * FROM table_request WHERE id = $id");
          $result = $query->fetch_object();

          $save = "INSERT INTO table_list(id, Name, Remark) VALUES ('".$result->id."','".$result->Name."','".$result->Remark."')";

          // For save data
          mysqli_query($conn, $save);

          // For delete data
          mysqli_query($conn, "DELETE FROM table_request WHERE id = $id");
       }

      header('Location:table_request.php');
   }

Leave a Reply

Your email address will not be published. Required fields are marked *