In this tutorial, we will create a Merge Two Table using MySQLi. This code will merge the two existing tables in the MySQLi server when the user clicks the merge button. The system uses a MySQLi SELECT() function and adds a LEFT JOIN parameter to merge the two existing tables into one that has the same key in both. This is a user-friendly kind of program feel free to the user in your application.
We will be using PHP as a scripting language that manages a database server to handle a bulk of data per transaction. It describes as an advanced technology that manages both server and control-block of your machine.
Table of Contents
Getting Started:
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/.
Creating Database
Open your database web server, then create a database name in it db_merge; after that, click Import, then locate the database file inside the folder of the application then click ok.
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_merge"); if(!$conn){ die(mysqli_error()); } ?>
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.
<?php require'conn.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 - Merge Two Table Using MySQLi</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-6"> <center><h4>Owner</h4></center> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> <?php $query=mysqli_query($conn, "SELECT * FROM `owner`") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $fetch['owner_id']?></td> <td><?php echo $fetch['owner_name']?></td> </tr> <?php } ?> </tbody> </table> <center><h4>Car</h4></center> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> <?php $query=mysqli_query($conn, "SELECT * FROM `car`") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $fetch['car_id']?></td> <td><?php echo $fetch['car_name']?></td> </tr> <?php } ?> </tbody> </table> </div> <div class="col-md-6"> <form method="POST" action=""> <center><button class="btn btn-primary" name="submit">Merge Table</button></center> </form> <br /> <?php include'merge.php'?> </div> </div> </body> </html>
Creating the Main Function
This code contains the main function of the application. This code will merge the two 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 merge.php.
<?php if(ISSET($_POST['submit'])){ ?> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th>ID</th> <th>Owner Name</th> <th>Car</th> </tr> </thead> <tbody> <?php $query=mysqli_query($conn, "SELECT * FROM `owner` LEFT JOIN `car` ON owner.car_id = car.car_id") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $fetch['owner_id']?></td> <td><?php echo $fetch['owner_name']?></td> <td><?php echo $fetch['car_name']?></td> </tr> <?php } ?> </tbody> </table> <?php } ?>
There you have it we successfully created Merge Two 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!