How to get last inserted ID and send again

769 views Asked by At

How can I send the lastInsertId again to another php document?

Explanation why: After someone sends a form (form.html) to a database (send.php) he will get an ID for that form. This ID I show in the send.php to the person via PDO:

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

At this confirmation page I want give the person the possibility to print the data from his form as an pdf. So I wrote:

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

But I he doesn't send the lastInsertId -> I guess the problem is here:

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

Can you help me to solve that problem?

3

There are 3 answers

0
SANM2009 On BEST ANSWER

Your code should be like this:

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

Not sure if it's the best way, but I once needed the last ID of a person who registered. I did the following after inserting the info into the DB (My table has Auto Increment Primairy Key ID):

$lastId = mysqli_insert_id($con);

You can store the ID anywhere you want. (In the URL or cookie)

Hope this helps (:

2
Jessy642 On

Thank you very much! That helped.

Is their an easy way that after he press print (now gets with the ID all the data from the database to show it at the site print.php - that part all works) and NOW DIRECTLY asks to save as pdf?