So here I made an object that's getting saved in my database. In the database another table row is made with an ID.
<?php
if(isset($_POST['verzenden'])) {
$required = array('fname', 'lname', 'email', 'adress', 'zipcode');
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "<div class='text-danger'>*All fields are required.</div>";
} else {
saveCustomer($_POST['fname'],$_POST['lname'],$_POST['email'],$_POST['adress'],$_POST['zipcode']);
header("Location: sushi.php");
}
}
?>
Here is the function for it:
function saveCustomer(string $fname, string $lname, string $email, string $adress, string $zipcode): void
{
global $pdo;
$sth = $pdo->prepare('INSERT INTO customer (fname, lname, email, adress, zipcode) VALUES (:f,:l,:e,:a,:z)');
$sth->bindParam("f", $fname);
$sth->bindParam("l", $lname);
$sth->bindParam("e", $email);
$sth->bindParam("a", $adress);
$sth->bindParam("z", $zipcode);
$sth->execute();
}
How can I fetch the user's ID I just made using this function so I can put it in a SESSION?
function getCustomer():array
{
global $pdo;
$sth = $pdo->prepare('SELECT * FROM customer order by date DESC');
$sth->execute();
return $sth->fetchAll(PDO::FETCH_CLASS, 'Customer');
}