How to Join in an Insert in MySQL With PHP

The MySQL "insert" statement inserts a new record into a database table. To use a "join" statement with your insert statement, you must use the insert statement to move content from one table to another. Use the "select" statement with the "insert" statement to insert several records at a time. After you create the query, use PHP to run the query on the server.

Instructions

    • 1

      Right-click the PHP page you want to edit and select "Open With." Click your preferred PHP editor.

    • 2

      Create the query string with the "join" and "insert" statement. The following query string is an example of an insert and join statement:

      $query = "insert into customer (select* from backup_customers bc join orders o on bc.customerid=o.orderid)";

      In this example, the query fills the customer table with a list of orders and customers from a backup table. This is a common way to refill tables when you must restore from a backup.

    • 3

      Create the connection to the database server. The following code creates the connection:

      mysql_connect("localhost", "user", "pass");
      mysql_select_db("mydb");

      Replace "user" and "pass" with the username and password for your server. Replace "mydb" with the database on which you want to execute your query.

    • 4

      Execute the query. After you make the connection to the server, the PHP language can run any query on the server. In this example, the query string is named "$query." Use the following code to run the query:

      $finished= mysql_query($query)

Related Searches:

References

Comments

Related Ads

Featured