How can we insert data from one table to another table in php?

I was trying to insert the data from one table to another, and this is the error message I get:

Error: INSERT INTO content2 [d1, d2, d3] VALUES [John, Doo, 24];

Unknown column 'John' in 'field list'Error: INSERT INTO content2 [d1, d2, d3] VALUES [Mary, Moe, 36];

Unknown column 'Mary' in 'field list'Error: INSERT INTO content2 [d1, d2, d3] VALUES [Julie, Dooley, 42]

Unknown column 'Julie' in 'field list'Error: INSERT INTO content2 [d1, d2, d3] VALUES [John, Doo, 24];

Unknown column 'John' in 'field list'Error: INSERT INTO content2 [d1, d2, d3] VALUES [Mary, Moe, 36];

Unknown column 'Mary' in 'field list'Error: INSERT INTO content2 [d1, d2, d3] VALUES [Julie, Dooley, 42];

Unknown column 'Julie' in 'field list'

Here is my php code:

//Get Connection
require_once['connect.php'];

//Get Data
$sql = "SELECT d1, d2, d3 FROM content";
$result = mysqli_query[$con, $sql];

if [mysqli_num_rows[$result] > 0] {
// output data of each row
    while[$row = mysqli_fetch_assoc[$result]] {
        $row1 = $row["d1"];
        $row2 = $row["d2"];
        $row3 = $row["d3"];

//Insert Data to another table
            $sql = "INSERT INTO content2 [d1, d2, d3]
            VALUES [$row1, $row2, $row3];";
            if [mysqli_multi_query[$con, $sql]] {
                echo "New records created successfully";
            } else {
                echo "Error: " . $sql . "
" . mysqli_error[$con]; } //echo "id: " . $row["d1"]. " - Name: " . $row["d2"]. " " . $row["d3"]. "
"; } } else { echo "0 results"; }

I couldn't figure out what the problem are. Please Help

The SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another table.

The INSERT INTO SELECT statement requires that the data types in source and target tables match.

Note: The existing records in the target table are unaffected.

INSERT INTO SELECT Syntax

Copy all columns from one table to another table:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:

INSERT INTO table2 [column1, column2, column3, ...]
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico

And a selection from the "Suppliers" table:

SupplierIDSupplierNameContactNameAddressCityPostal CodeCountry
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Londona EC1 4SD UK
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA
3 Grandma Kelly's Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA

SQL INSERT INTO SELECT Examples

The following SQL statement copies "Suppliers" into "Customers" [the columns that are not filled with data, will contain NULL]:

Example

INSERT INTO Customers [CustomerName, City, Country]
SELECT SupplierName, City, Country FROM Suppliers;

Try it Yourself »

The following SQL statement copies "Suppliers" into "Customers" [fill all columns]:

Example

INSERT INTO Customers [CustomerName, ContactName, Address, City, PostalCode, Country]
SELECT SupplierName, ContactName, Address, City, PostalCode, Country FROM Suppliers;

Try it Yourself »

The following SQL statement copies only the German suppliers into "Customers":

Example

INSERT INTO Customers [CustomerName, City, Country]
SELECT SupplierName, City, Country FROM Suppliers
WHERE Country='Germany';

Try it Yourself »



You can copy one table to another using an MYSQL query and PHP script. if you are working on a PHP project and you want to copy a table data to another using MYSQL and PHP then you have done it by myself insert and select query. Think about the update operation in php. How we can get the data from the database user id. If we click id 1 that shown us id 1 data and the same for all id values like 1,2,3....etc. Now we will get the data using id and insert [copy ] into another table. You can get data by id wise. This will help you select an option in your web application. To copy one MySQL table to another table you have to go through the MYSQL syntax.


MYSQL copy one table to another SYNTAX



INSERT INTO table 2 
SELECT * FROM  table 1  WHERE ...

Using the above syntax we will copy one table data to another table. First of all, we will select the data from table 1 and insert into table 2. It will be done by the PHP script.

Now we will create a PHP script for copy one table data to another user id. As you know how to delete data from a table using id. We use the id and delete one by one row. If you want to copy and id to another table. You have to follow delete or update operation. In the delete or update operation, we get the data by id and perform delete and update operation. We will do the same thing here. We will get the data by id and insert into another table. That is a simple process of copy and gettable data by id .

Why need to copy data by id?
If you are working on a PHP project like contest, winner and same like these projects and you want to select one winner. This is done by the id. We create a database table named "winner and we will get the old table data by id and insert [copy] to MySQL winner table. This will help you to select the winner from the registered competitors [or according to your project etc ].

Copy one mysql table data to another mysql table using id

1. First of all create a table table 1 . [like competitors ]

2. Now create an another table with same table field [same as competitors table fields ]

3. Create a connection .

4. Select data by id . like -->

Chủ Đề