When you go to my website, i have a homepage with a button: Start enquete. When you click that button, you will see the enquete.
In the meantime, when you clicked that button. A ID has been inserted in the database.
Index.php:
<form method="POST">
<input type="hidden" name="x">
<input type="submit" value="Go to enquete">
</form>
<?php
if(isset($_POST['x'])){
$test = $database->insert_user_id();
//header('Location: enquete/page1.php');
}
And the function:
function insert_user_id(){
$sql = "INSERT INTO user (user_id) VALUES (DEFAULT)";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $this->pdo->('user_id');
}
the return $this->pdo->('user_id'); is something i'm testing with!
Now my question is. How do i return the last inserted id from the table user.
I need to show it on the next page. (See //header).
I can't do this with a other query like: SELECT MAX ID or GetLastInsertId.
This becuase, when some one else also starts the enquete, he will have an other id all of the sudden.
How do i let the person keep the correct id.
Use PDO::lastInsertId
$this->pdo->lastInsertId();
Try this. Manual PDO::lastInsertId
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With