Home » Php » php – I Want to move data from one table to another

php – I Want to move data from one table to another

Posted by: admin February 25, 2020 Leave a comment

Questions:
PHP Code in codecancel.php:


 <?php 

if(isset($_POST['codecancel_btn']))
{

$con = new mysqli('localhost', 'root', '', 'halcondentalclinic');

if($con->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$id=$_POST['codecancel_id'];
$sql =   "INSERT INTO cancelled_table SELECT * FROM bookings WHERE booking_id='$id';";



            if ($con->query($sql) === TRUE) {
                $query = "DELETE FROM bookings WHERE booking_id='$id' ";
                if ($con->query($query) === TRUE) {
                    $message = "Success!";

                } else {
                    echo "Error: " . $query . "<br>" . $con->error;
                }
            } else {
                echo "Error: " . $sql . "<br>" . $con->error;
            }
}
?>

For my Table appointment_calendar.php

 <?php 
    if(mysqli_num_rows($query_run)>0)
    {
        while($row= mysqli_fetch_assoc($query_run))
        {
            ?>


     <tr>
        <td><?php echo $row['booking_id']; ?> </td>
        <td> <?php echo $row['name']; ?></td>
        <td><?php echo $row['email'];?></td>
        <td><?php echo $row['timeslot'];?> </td>
         <td><?php echo $row['date'];?> </td>
        <td>
            <form action="codecancel.php" method="post">
                <input type="hidden" name="codecancel_id" value="<?php echo $row['booking_id']; ?> ">
                <button  type="submit" name="codecancel_btn" class="btn btn-danger"> CANCEL</button>
            </form>
        </td>

      </tr>
    <?php
        }
    }
    else {

        echo" No Appointment Found";
    }
 ?>

I want to transfer/move data if the patient cancels his appointment and the cancel appointment move to cancelled_table in the database then the canceled appointment will be removed to the bookings table in the database this is my table I want to cancel.
enter image description here

if I clicked the cancel button the error is :
enter image description here

Sorry guys I am Newbie in PHP AND MYSQL 🙂

How to&Answers:
INSERT INTO cancel_table(Booking_ID, Name ,Email,Date) 
SELECT id as Booking_id, Name ,Email,Date  
FROM bookings 
WHERE id = 1

try This And Take care To standard of col Name in DB