this is the database file that has db connections it is called db.php
<?php
class Database
{
private $dsn = "mysql:host=localhost;dbname=testmovie";
private $user = "root";
private $pass = "";
public $conn;
function __construct()
{
try {
$this->conn = new PDO($this->dsn,$this->user,$this->pass);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
function to insert details and return lastInsertId
public function booking($fname,$lname,$tseats,$show_id,$email,$phone,$price)
{
$sql ="INSERT INTO bookings
(firstname,lastname,seats_booked,show_id,email,phone,price)
VALUES (:fname, :lname, :tseats, :show_id, :email,
:phone, :price)";
$stmt= $this->conn->prepare($sql);
$stmt->execute(['fname'=>$fname, 'lname'=>$lname,
'tseats'=>$tseats, 'show_id'=>$show_id,
'email'=>$email, 'phone'=>$phone,
'price'=>$price]);
$last= $stmt->lastInsertId();
return $last;
}
}
?>
A new php file that prints value of the last id entered
<?php
require_once 'db.php';
$db= new Database();
the variable are fetched form input fields.
if ($last=$db->booking($fname,$lname,$tseats,$show_id,$email,$phone,$price)) {
echo $last;
}
?>