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);