Insert data from one table to another table using PHP
Last Updated :
07 Apr, 2021
In this article, we are going to insert data into another table from the existing table using PHP.
Requirements:
PHP stands for Hypertext preprocessor. MySQL is a database query language for performing database operations. We are going to insert data from one table into another table by using MySQL Server.
MySQL server is an open-source relational database management system that is used for web applications.
Follow the below Steps:
- Write create table2 in XAMPP SQL server in the same database geeks_database

- Now insert records in table 2 using PHP code from table1.
PHP Code:
PHP
<?php
$connection_link = new mysqli("localhost", "root", "","geeks_database");
if ($connection_link === false) {
die("ERROR: Not connected. ".$connection_link->connect_error);
}
$sql_query = "insert table2 select * from table1";
if ($connection_link->query($sql_query) === true)
{
echo "Data Copied Successfully.";
}
else
{
echo "ERROR: Could not able to proceed $sql_query. "
.$connection_link->error;
}
$connection_link->close();
?>
|
- Save this code as copying_data.php under xampp->htdocs folder.

-
Output:
- Open web browser and type “http://localhost/copying_data.php“.

data copied image
- Finally, view your table2. The data is copied from table1 successfully.

Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...