[英]Getting the last inserted row after doing an INSERT in PDO (with MySQL)?
[英]how to insert multiple row after getting last inserted row id
我想將一些數據插入表(一行)中,然后在此之后,將一些行插入與A_id與表A相關的其他表B中。 因此,我想獲取表AI的當前行ID已插入數據,然后使用它將數據放入其他表中。
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())
}
最后插入的ID每秒都在變化。因此不適用於我所需的A_id
您應該嘗試如下示例
$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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.