简体   繁体   中英

how to insert multiple row after getting last inserted row id

I want to insert some data into a table (one row) then after this ended , I want to insert some rows into other table B which is related to table A with A_id . So I want to get current row id of table AI have inserted data then use it to put data into other table.

INSERT INTO tableA (titleA)
  VALUES('test title A')


for ($i=0 ; $i<9 ; $i++){
INSERT INTO b_shop_option (titleB,A_id) VALUES ('$title[$i]',LAST_INSERT_ID())
}

last inserted id is changing every second.so it doesn't work for my required A_id

You should try like as below example

   $q1 = INSERT INTO tableA (titleA)
  VALUES('? ?');
  $stmt->bind_param("ss", 'test', 'Title A');

$q1_last_inserted_id = $your_conncetion_variable->lastInsertId();

for ($i=0 ; $i<9 ; $i++){
INSERT INTO b_shop_option (titleB,A_id) VALUES ('$title[$i]',$q1_last_inserted_id)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM