Skip to main content

PHP - Merge Two Table Using MySQLi


In this tutorial we will create a Merge Two Table using MySQLi. This code will merge the two existing table in the MySQLi server when user click the merge button. The code use a MySQLi SELECT() function and add a LEFT JOIN parameter to merge the the two existing table into one that has a same key in both. This is a user-friendly kind of program feel free user it in your program.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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.
tut1

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.

  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_merge");
  3.  
  4. if(!$conn){
  5. }
  6. ?>

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.

  1. <?php require'conn.php'?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Merge Two Table Using MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-6">
  19. <center><h4>Owner</h4></center>
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>ID</th>
  24. <th>Name</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <?php
  29. $query=mysqli_query($conn, "SELECT * FROM `owner`") or die(mysqli_error());
  30. while($fetch=mysqli_fetch_array($query)){
  31. ?>
  32. <tr>
  33. <td><?php echo $fetch['owner_id']?></td>
  34. <td><?php echo $fetch['owner_name']?></td>
  35. </tr>
  36. <?php
  37. }
  38. ?>
  39. </tbody>
  40. </table>
  41. <center><h4>Car</h4></center>
  42. <table class="table table-bordered">
  43. <thead class="alert-info">
  44. <tr>
  45. <th>ID</th>
  46. <th>Name</th>
  47. </tr>
  48. </thead>
  49. <tbody>
  50. <?php
  51. $query=mysqli_query($conn, "SELECT * FROM `car`") or die(mysqli_error());
  52. while($fetch=mysqli_fetch_array($query)){
  53. ?>
  54. <tr>
  55. <td><?php echo $fetch['car_id']?></td>
  56. <td><?php echo $fetch['car_name']?></td>
  57. </tr>
  58. <?php
  59. }
  60. ?>
  61. </tbody>
  62. </table>
  63. </div>
  64. <div class="col-md-6">
  65. <form method="POST" action="">
  66. <center><button class="btn btn-primary" name="submit">Merge Table</button></center>
  67. </form>
  68. <br />
  69. <?php include'merge.php'?>
  70. </div>
  71. </div>
  72. </body>
  73. </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.

  1. <?php
  2. if(ISSET($_POST['submit'])){
  3. ?>
  4. <table class="table table-bordered">
  5. <thead class="alert-info">
  6. <tr>
  7. <th>ID</th>
  8. <th>Owner Name</th>
  9. <th>Car</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <?php
  14. $query=mysqli_query($conn, "SELECT * FROM `owner` LEFT JOIN `car` ON owner.car_id = car.car_id") or die(mysqli_error());
  15. while($fetch=mysqli_fetch_array($query)){
  16. ?>
  17. <tr>
  18. <td><?php echo $fetch['owner_id']?></td>
  19. <td><?php echo $fetch['owner_name']?></td>
  20. <td><?php echo $fetch['car_name']?></td>
  21. </tr>
  22. <?php
  23. }
  24. ?>
  25. </tbody>
  26. </table>
  27. <?php
  28. }
  29. ?>

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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.