Welcome to Techno Smarter QA, Question, Answer forum. Ask questions, receive answers, or help someone. Join Now
+4 votes

This is table 1 which i want to copy its data and paste into another table. I mean, I want to copy one table data and paste into another table in PHP with MYSQL. 

My first MYSQL database table -

My second MYSQL database table. 

How can I copy one table data into another table via PHP and MYSQL database table. 

in PHP by (90 points) 17 31
reshown by

3 Answers

+2 votes

Copy one table data to another using PHP.

To copy one MySQL table data in another table MYSQL  query is to be used. To copy the data from one table to another table, you have to perform the query of MYSQL  in such a way that your problem is solved. In the query of MYSQL, you have to select the data from a table and You have to copy (insert) the other table. To copy the data table of the Msql table, you have to do the following query to MYSQL.

INSERT INTO table 2 

SELECT * FROM  table 1  WHERE ...

How to MySQL copy query will work. 

Insert into table 2 - Insert the data into table 2 

SELECT * FROM  table 1  WHERE ..- Select the data from table From where do you want to copy. 

You have to need to use this MYSQL query to perform the copy operation one table to another.

I have a reference for you. 


by (8.9k points) 10 23 62
+1 vote

Copy one table data into another table is pretty simple using the MYSQL query. First of all, understand the thing because you want to select the data and insert into another MYSQL database table using PHP. It's not a big deal, you can modify query according to your need. 

Use the below query to insert and select data. 


SELECT * FROM  table1 ; 

The table is another table in which you want to insert the data from table one in PHP. The table1 specifies the table from which you want to select data and paste (insert) data into another table.

You can use Where clause with the select query. 


SELECT * FROM  table1 WHERE  ;

 WHERE clause helps to select the specific id-data or other. You can change according to your need. 

by (7k points) 9 27 62
This is my code. I used to insert Into another table

$result = "INSERT INTO voucher_m(mainhead,subhead,dr,cr) VALUES('$mainhead','$subhead','$dr','$cr'); INSERT INTO voucher_m(voucherno,type) VALUES('$Voucherno','$type') SELECT * FROM next;";
       mysqli_multi_query($cser, $result);
Why are you using insert query two times?
+1 vote

If you want to insert data from another table, you have to check and specify the columns too. The MYSQL database table layouts can be different, you should understand this concept and specify the columns (fields ) 

INSERT INTO table2(column1, column1, column1)
SELECT column5, column7, column8 FROM table1 WHERE id = 1
by (8.9k points) 10 23 62