How to insert data in two tables at the same time

Source: Internet
Author: User

Click Submit Order, how to make the two tables at the same time, Order_goods table, order_id,goods_id is the order table and goods table of foreign keys, how to do, no idea

Reply content:

Click Submit Order, how to make the two tables at the same time, Order_goods table, order_id,goods_id is the order table and goods table of foreign keys, how to do, no idea

The product list is already in place, so insert the order form at the time of placing it, and insert the order item form within the same transaction. Due to foreign keys, orders can only be ordered after order goods.

MySQL cannot implement simultaneous insertion of two tables, and can guarantee consistency with transactions.

Same question
How do you determine if the Insert Order table operation was successful?
That is, inserting the order form and inserting the order product list to execute the success at the same time OH
Any special knowledge?
Is that okay?
First gets the ID after the insert order is executed, if the order table ID is self-increment, the execution succeeds returns the ID of the row record that was inserted successfully, and if the insert fails, returns 0, and then I judge if I need to execute the Insert Order Product table According to this ID?
Is that the case?

If the data table doesn't work with the primary foreign key, use two SQL statements.
Whether the two inserted IDs are at the same time determining if the insert succeeds
If an insert does not successfully delete the inserted data successfully

MySQL transactions can be used to ensure consistency of data;

$handler =mysql_connect ("localhost", "root", "password");
mysql_select_db ("task");
mysql_query ("Set autocommit=0");//set to not auto-commit because MySQL defaults to execute immediately
mysql_query ("Begin");//Start Transaction definition
if (!mysql_query ("INSERT into trans (ID) VALUES (' 2 ')")
{
mysql_query ("ROLLBACK");//Determine rollback when execution fails
}
if (!mysql_query ("INSERT into trans (ID) VALUES (' 4 ')")
{
mysql_query ("ROLLBACK");//Judgment execution failed rollback
}
mysql_query ("COMMIT");//Perform Transaction
Mysql_close ($handler);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.