<?php
// Assuming $mysqli is already defined and connected to the database
// Function to handle the song reservation
function reserveSong($mysqli, $song_id) {
// SQL query to insert the reservation
$sql = "INSERT INTO reservations (song_id) VALUES (?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i', $song_id);
$stmt->execute();
$stmt->close();
}
// Function to display reserved songs
function displayReservedSongs($mysqli) {
// SQL query to get reserved songs
$sql = "
SELECT songs.name AS song_name, singers.name AS singer_name
FROM reservations
JOIN songs ON reservations.song_id = songs.id
JOIN singers ON songs.singer_id = singers.id
";
$result = $mysqli->query($sql);
// HTML output
echo "<h1>Reserved Songs</h1>";
echo "<table border='1'>";
echo "<tr><th>Song Name</th><th>Singer Name</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['song_name'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['singer_name'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "</tr>";
}
echo "</table>";
$result->free();
}
// Check if a song_id has been submitted for reservation
if (isset($_POST['song_id'])) {
$song_id = $_POST['song_id'];
reserveSong($mysqli, $song_id);
}
// Display the reserved songs
displayReservedSongs($mysqli);
?>
<!-- HTML form to reserve a song -->
<form action="" method="post">
<label for="song_id">Select Song ID to Reserve:</label>
<input type="number" id="song_id" name="song_id" required>
<input type="submit" value="Reserve">
</form>