Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dbServername = "localhost";
- $dbUsername = "root";
- $dbPassword = "";
- $dbName = "assignment2";
- $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName)
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
- <title>Assignment 2</title>
- </head>
- <body style="margin: 2%;">
- <form method="post">
- Choose a class:
- <select name="course">
- <option value="creative_cupcakes">Creative Cupcakes</option>
- <option value="digital_photography">Digital Photography</option>
- <option value="family_history">Family History for Beginners</option>
- <option value="acrylic_painting">Fundamentals of Acrylic Painting</option>
- <option value="holiday_french">Holiday French</option>
- </select>
- <br />
- <input type="submit" value="Select">
- </form>
- <br />
- <?php
- // If the user's picked a course, show the next form
- if (isset($_POST['course'])) {
- $title = $_POST["course"];
- $GLOBALS["course"] = $title;
- // Grab the selected course for the slots where capacity is above 0
- $sql = "SELECT * FROM courses WHERE title='$title' AND capacity > 0";
- $result = mysqli_query($conn, $sql);
- // Render the form for each slot
- echo "<form method='post'> Choose a timeslot for ".$title;
- echo "<br />";
- while($row = mysqli_fetch_assoc($result)) {
- echo "<input type='radio' name='slot' value='".$row['slot']."'>";
- echo $row['slot']." - ".$row['capacity']." spaces left";
- echo "</input> <br/>";
- }
- echo "<input type='hidden' name='course' value='".$title."'>";
- echo "<br/ > Name: <input type='text' name='name'> <br />";
- echo "Phone Number: <input type='text' name='number'> <br /> <br />";
- echo "<input type='submit' value='Submit'> </form>";
- }
- ?>
- <?php
- // If the user's picked a slot, run the confirmation
- if (isset($_POST['name']) && isset($_POST['number']) && isset($_POST['slot'])) {
- $course = $_POST['course'];
- $name = $_POST['name'];
- $number = $_POST['number'];
- $slot = $_POST['slot'];
- // Validate name
- if (ereg("[\sa-zA-z'-]", $name)) {
- // Validate number
- if (is_numeric($number)) {
- // Insert new booking
- $sql = "INSERT INTO bookings (course, name, number, slot) VALUES ('$course', '$name', $number, '$slot')";
- mysqli_query($conn, $sql);
- // Update existing capacity
- $sql = "SELECT capacity FROM courses WHERE title='$title' && slot='$slot' LIMIT 1";
- $result = mysqli_query($conn, $sql);
- $row = mysqli_fetch_assoc($result);
- $capacity = $row['capacity'] - 1;
- $sql = "UPDATE courses SET capacity=$capacity WHERE title='$title' && slot='$slot'";
- mysqli_query($conn, $sql);
- echo "Booking successful!";
- } else {
- echo "Invalid number";
- }
- } else {
- echo "Invalid name";
- }
- }
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment