如何获取最后插入的 ID 并再次发送

How to get last inserted ID and send again

如何将 lastInsertId 再次发送到另一个 php 文档?

解释原因: 在某人将表单 (form.html) 发送到数据库 (send.php) 后,他将获得该表单的 ID。这个 ID 我在 send.php 中通过 PDO 显示给此人:

<p>Your ID:<?php echo $dbh->lastInsertId(); ?></p>

在此确认页面上,我想让此人能够将其表格中的数据打印为 pdf 格式。所以我写道:

<form action="print.php" method="POST"> 
<input type="hidden" name="ID" value="<=htmlspecialchars($_POST['lastInsertId']);?>"/>
<input type="submit" name="Print" value="Print" >
</form>

但我他没有发送 lastInsertId -> 我想问题出在这里:

value="<?=htmlspecialchars($_POST['lastInsertId']);?>"

你能帮我解决这个问题吗?

不确定这是否是最好的方法,但我曾经需要一个注册人的最后一个 ID。 将信息插入数据库后,我做了以下操作(我的 table 具有自动增量主键 ID):

$lastId = mysqli_insert_id($con);

您可以将 ID 存储在任何您想要的位置。 (在 URL 或 cookie 中)

希望对您有所帮助 (:

你的代码应该是这样的:

<form action="print.php" method="POST"> 
<input type="hidden" name="ID" value="<?php echo $dbh->lastInsertId(); ?>"/>
<input type="submit" name="Print" value="Print" >
 </form>

非常感谢!那有帮助。

他们是一种简单的方法,在他按下打印后(现在使用 ID 从数据库中获取所有数据以在网站上显示它 print.php - 这部分都有效)并且现在直接要求保存作为 pdf?