Insert data from one table to another table using PHP Last Updated : 07 Apr, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to insert data into another table from the existing table using PHP. Requirements: XAMPP Webserver 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. Insert query syntax: insert table_2 select * from table_1. Follow the below Steps: Open XAMPP server and start Apache and MySQL. Open your browser and type "localhost/phpmyadmin". Create a database named "geeks_database" Now create a table named table1 with 4 columns and click on save. Now open the SQL column in the database server and insert records into it. MySQL code: The following are the SQL statements to insert data in table1. INSERT INTO `table1`(`name`, `address`, `country`, `city`) VALUES ('sravan','kakumanu','india','guntur'); INSERT INTO `table1`(`name`, `address`, `country`, `city`) VALUES ('sudheer','chebrolu','india','guntur'); INSERT INTO `table1`(`name`, `address`, `country`, `city`) VALUES ('vani','kakumanu','india','guntur'); INSERT INTO `table1`(`name`, `address`, `country`, `city`) VALUES ('radha','tenali','india','guntur'); Output: The table1 includes the following data. 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 // creating a connection by passing server name, // username, password and database name // servername=localhost // username=root // password=empty // database name= geeks_database $connection_link = new mysqli("localhost", "root", "","geeks_database"); if ($connection_link === false) { die("ERROR: Not connected. ".$connection_link->connect_error); } //sql query to perform copying data from one table to another $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; } // Close the connection $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. Comment More infoAdvertise with us Next Article Insert data from one table to another table using PHP S sravankumar_171fa07058 Follow Improve Article Tags : PHP PHP-MySQL PHP-Questions Similar Reads How to Update Data in MySQL Database Table Using PHP? Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection 3 min read How to retrieve data from MySQL database using PHP ? There are steps to understand for retrieving the data from the MySQL database. Approach: Create the database, then create the table for data.Enter the rows in the table. You have to connect to the database. Now we understand each and every step as shown below.  Example 1: In this. we use PHPMyAdmin 2 min read How to Insert JSON data into MySQL database using PHP? To insert JSON data into MySQL database using PHP, use the json_decode function in PHP to convert JSON object into an array that can be inserted into the database. Here, we are going to see how to insert JSON data into MySQL database using PHP through the XAMPP server in a step-by-step way. JSON Str 3 min read CRUD Operation in MySQL Using PHP, Volley Android - Read Data In the previous article, we have performed the insert data operation. In this article, we will perform the Read data operation. Before performing this operation first of all we have to create a new PHP script for reading data from SQL Database. Prerequisite: You should be having Postman installed i 9 min read How to execute an SQL query and fetch results using PHP ? In this article, we will discuss how to execute an SQL query and how to fetch its result? We can perform a query against the database using the PHP mysqli_query() method. Syntax: We can use the mysqli_query( ) method in two ways: Object-oriented styleProcedural style Parameters: connection: It is r 3 min read Like