How to Transfer Data To Other Table in PHP

July 9, 2020
PHP
how to transfer data to other table in php

How to Transfer Data To Other Table in PHP with Source Code

In this tutorial, we will create a how to transfer data to other table in php using MySQLi. This code will move the data to another table when the user click the move button. The system uses MySQLi SELECT to fetch the data that will be transferred to another table and be deleted after transfer using the DELETE query. This a user-friendly program. Feel free to modify and use it for your system.

We will be using PHP as a scripting language that is interpreted in the web server, such as xamp, wamp, etc. It is widely used by modern website application to handle and protect user confidential information.

Getting Started on How to Transfer Data To Other Table in PHP

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Lastly, this is the link for the jquery that I usedhttps://jquery.com/.

Creating Database

Open your database web server then create a database name in it db_transfer; after that, click Import, then locate the database file inside the folder of the application then click ok.

how to simple transfer data to other table in php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

<?php
    $conn=mysqli_connect("localhost", "root", "", "db_transfer");
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Simple Transfer Data To Other Table</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <button class="btn btn-primary" type="button" data-toggle="modal" data-target="#form_modal">Add member</button>
        <br /><br />
        <div class="col-md-6">
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Address</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        require'conn.php';
                        $query=mysqli_query($conn, "SELECT * FROM `member1`") or die(mysqli_error());
                        while($fetch=mysqli_fetch_array($query)){
                    ?>
                    <tr>
                        <td><?php echo $fetch['firstname']?></td>
                        <td><?php echo $fetch['lastname']?></td>
                        <td><?php echo $fetch['address']?></td>
                        <td><a href="transfer.php?mem_id=<?php echo $fetch['mem_id']?>" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-right"></span></a></td>
                    </tr>
                    <?php
                        }
                    ?>
                </tbody>
            </table>
        </div>
        <div class="col-md-1"></div>
        <div class="col-md-5">
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        require'conn.php';
                        $query=mysqli_query($conn, "SELECT * FROM `member2`") or die(mysqli_error());
                        while($fetch=mysqli_fetch_array($query)){
                    ?>
                    <tr>
                        <td><?php echo $fetch['firstname']?></td>
                        <td><?php echo $fetch['lastname']?></td>
                        <td><?php echo $fetch['address']?></td>
                    </tr>
                    <?php
                        }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<div class="modal fade" aria-hidden="true" id="form_modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="save.php">
                <div class="modal-header">
                    <h3 class="modal-title">Add Member</h3>
                </div>
                <div class="modal-body">
                    <div class="col-md-2"></div>
                    <div class="col-md-8">
                        <div class="form-group">
                            <label>Firstname</label>
                            <input type="text" name="firstname" class="form-control" required="required"/>
                        </div>
                        <div class="form-group">
                            <label>Lastname</label>
                            <input type="text" name="lastname" class="form-control" required="required"/>
                        </div>
                        <div class="form-group">
                            <label>Address</label>
                            <input type="text" name="address" class="form-control" required="required"/>
                        </div>
                    </div>
                </div>
                <br style="clear:both;"/>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
                    <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
                </div>
            </form>
        </div>
    </div>
</div>	

Creating the PHP Query

This code contains the php query of the application. This code will store the user data inputs to the database server. To make this just copy and write these block of codes below inside the text editor, then save it as save.php.

<?php
    require_once'conn.php';
 
    if(ISSET($_POST['save'])){
        $firstname=$_POST['firstname'];
        $lastname=$_POST['lastname'];
        $address=$_POST['address'];
 
        mysqli_query($conn, "INSERT INTO `member1` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
 
        header("location: index.php");
    }
?>

Creating the Main Function

This code contains the main function of the application. This code will move the data in the table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as transfer.php

<?php
    require_once'conn.php';
 
    if(ISSET($_REQUEST['mem_id'])){
        $mem_id=$_REQUEST['mem_id'];
 
        $query=mysqli_query($conn, "SELECT * FROM `member1` WHERE `mem_id`='$mem_id'") or die(mysqli_error());
        $fetch=mysqli_fetch_array($query);
        $firstname=$fetch['firstname'];
        $lastname=$fetch['lastname'];
        $address=$fetch['address'];
 
 
        mysqli_query($conn, "INSERT INTO `member2` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
        mysqli_query($conn, "DELETE FROM `member1` WHERE `mem_id`='$mem_id'") or die(mysqli_error());
 
        echo"<script>alert('Data successfully transfer')</script>";
        echo"<script>window.location='index.php'</script>";
    }
?>

There you have it we successfully created Simple Transfer Data To Other Table using MySQLi. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments
  • can you please share the database file of this code

    Aakil August 20, 2022 8:47 pm Reply

Leave a Reply

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