Merge Two Table Using MySQLi in PHP

By CampCodes Administrator

Published on:

merge two table using mysql in php

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.

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.

merge two table using mysql 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_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.

READ ALSO:   How to Update Image in PHP with Demo in MySQL Database

<?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
    }
?>
create multiple tables mysql using php cross join oracle full inner join full outer join mysql how can i join two tables in mysql? how can i join two tables in php? how can i merge two tables in database? how merge works in mysql how to fetch data from three tables in mysql using php how to fetch data from two tables in mysql using php how to fetch data from two tables in mysql without join how to fetch data from two tables in mysqli using php how to fetch data from two tables in sql with join how to join 4 tables in sql query how to join three tables in mysql in php how to join three tables in sql using joins how to join two table in php how to join two tables in mysql how to join two tables in mysql query how to join two tables in mysql using php with example how to join two tables in mysql with where clause how to join two tables in phpmyadmin how to link tables in mysql how to link two tables in mysql how to merge two tables in sql how to retrieve data from 4 tables in sql how to retrieve data from multiple tables in mysql how to retrieve data from multiple tables in sql how to search data from multiple table in mysql how to search data from multiple table in mysql using php how to select data from two tables in mysql php inner join inner join 3 tables inner join mysql join join in php mysql join on multiple columns sql join tables from different databases mysql joining multiple tables using mysql left inner join left join left join mysql left join on multiple columns mariadb merge two tables merge left sql merge mysql merge two database tables mysql merge two table using mysql in php multiple left joins sql mysql combine multiple tables into one mysql combine two queries mysql combine two tables mysql compare two tables and merge mysql compare two tables from different databases mysql concat two columns from different tables mysql create relationship between two tables mysql cross join mysql full outer join mysql get data from two tables mysql inner join mysql inner join 3 tables mysql join mysql join 2 tables mysql join columns from multiple tables mysql join example mysql join multiple tables mysql join on two columns mysql join syntax mysql join tables mysql join two tables mysql join two tables by id mysql join two tables with different columns mysql join types mysql joins mysql joins explained mysql left join mysql left join multiple tables mysql left join not working mysql left join null mysql left join with condition mysql left join with where clause mysql merge tables example mysql merge tables from different databases mysql merge tables with different columns mysql merge two tables with different columns mysql merge two tables with same columns mysql outer join mysql outer join syntax mysql query examples multiple tables mysql query from two tables mysql query two tables mysql search multiple tables mysql search multiple tables in one query mysql select columns from multiple tables mysql select from multiple tables mysql select from multiple tables without join mysql select from two tables at once mysql select merge two tables mysql select multiple columns from different tables mysql tutorial mysql update outer join in sql outer join mysql php mysql join multiple tables php mysql left join where clause php mysql search multiple tables using a keyword php mysql select from multiple tables php sql join php tutorials phpmyadmin merge two tables query multiple tables mysql select data from multiple tables mysql select data from multiple tables mysql php select from 3 tables mysql select from multiple tables sql select from two tables with same column name mysql sql combine two tables sql inner join sql join sql join 2 tables sql join 3 tables sql join 4 tables sql join multiple tables sql join multiple tables with conditions sql join where sql join with where clause example sql joins sql joins explained sql left join sql merge two tables with different fields sql merge two tables with same columns sql multiple joins sql on sql outer join sql query examples from multiple tables sql select from multiple tables without join sql union when to use cross join

Leave a Comment