Retrieving the last insert id from MySql using PHP

前端 未结 7 1053
时光说笑
时光说笑 2020-12-22 08:38

I have two tables in MySql database, one is sales_order and the other is sales_order_details which contains the details of the order in the s

7条回答
  •  囚心锁ツ
    2020-12-22 09:26

    There are 2 ways which you could solve this:

    After inserting the entry in the sales_order:

    1. Get the last_insert_id and store it in a php variable, then inject this value into the relevant queries.

    2. Store the last_insert_id in a MySql user-defined variable and use the variable in the query instead of last_insert_id.

    sample code for option 2

    SET @last_order_id = last_insert_id();

    insert into sales_order_details (order_id, prod_id, prod_price)
    values (@last_order_id, 5, 1500);`
    
    insert into sales_invoice (order_id, invoice_id)
    values (@last_order_id, 1);`
    

提交回复
热议问题