1 回答

TA貢獻1856條經驗 獲得超11個贊
您無法獲取下一個 id,您可以做的是執行不帶 :nextRow (null 或其他臨時值)的 INSERT,存儲插入行的 id 并在下一個循環中運行插入新行并用此更新上一個行新行 ID 作為 nextRow。
像這樣的東西:
<?php
$stmt = $pdo->prepare("INSERT INTO row (name, prev_row, next_row) VALUES (:name, :prevRow, :nextRow");
$stmtUpdate = $pdo->prepare("UPDATE row SET next_row = :nextRow WHERE id = :id");
$previousId = null;
foreach ($rows as $row) {
// insert current row without nextRow
$stmt->bindParam(":name", $row->getName());
$stmt->bindParam(":prevRow", $pdo->lastInsertID());
$stmt->bindParam(":nextRow", null);
$stmt->execute();
if ($previousId) {
// update previous row with this rows id as its nextRow
$stmtUpdate->bindParam(":nextRow", $pdo->lastInsertId());
$stmtUpdate->bindParam(":id", $previousId);
$stmtUpdate->execute();
}
$previousId = $pdo->lastInsertID();
}
這也是有道理的,因為您插入的最后一行不會有nextRow
- 1 回答
- 0 關注
- 131 瀏覽
添加回答
舉報