I have two tables. An "events" tables where events have a unique event_id and where each events has a total_tickets available. I then have a "bookings" table where event_id is used as a foreign key. For each booking, I have a tickets_booked value. I would like add all the tickets_booked for a specific event_id in my "bookings" table and compare it to the total_tickets value from my "events" table for that same event_id.
I basically want to warn the user when an event is sold out.
So far I have 2 queries but I can seem to combine them.
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['book'])) {
$num_tick = $_POST['num_tick'];
$booked = "SELECT SUM(booked_tickets) AS sum_tickets FROM bookings
WHERE event_id = '$event_id'";
$result = $conn->query($booked);
$total = "SELECT total_tickets FROM events
WHERE event_id = '$event_id'";
$result2 = $conn->query($total);
if (($result + $num_tick) < $result2) {
$sql = "INSERT INTO bookings (user_id, event_id, booked_tickets)
VALUES ((SELECT user_id FROM users WHERE username = '$user'), '$event_id', '$num_tick')";
if ($conn->query($sql) === TRUE) {
header('Location: dashboard.php?username=' . $user);
}
}
I know this is absolutely not correct, but this is the logic I have so far :D Any help would be appreciated !