PHP - Merge Two Table Using MySQLi

Submitted by: 
Visitors have accessed this post 334 times.

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

And, this is the link for the bootstrap that i used for the layout design

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.

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

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.