Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Games Library Reservations Page</title>
- <link href="styles.css" rel="stylesheet" type="text/css">
- </head>
- <body>
- <style>
- .error {color: #FF0000;}
- </style>
- <div id='main'>
- <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
- <div id='menu-wrapper'>
- <div id='menu'>
- <ul>
- <li><a href='index.php'>Home</a></li>
- <li class='current_page_item'><a href='#'>Reservations</a></li>
- </ul>
- </div>
- </div>
- <div>
- <h1>Reservations form</h1>
- <h4> Welcome to the reservation page here you can reserve your choice of game</h4>
- </div>
- <?php
- $file = fopen("games.csv","r");
- //stores it in games
- if(fopen("games.csv","r")) {
- while(! feof($file)){
- //puts games into an array
- $games[] = fgetcsv($file);
- }
- //count the amount of rows
- $num_rows = count($games);
- //create the variables for the input values and give it a value of nothing
- $first_name = $last_name = $gameID = $start_reservation = $num_days = "";
- //create variabls to store the message that will display when an error is met
- $first_nameErr = $last_nameErr = $gameIDErr = $start_reservationErr = $num_daysErr = "";
- //error catching- this part checks to see if a value hasd been inputed for all the variables
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["first_name"])) {
- //displays an error message
- $first_nameErr = "first name is required";
- } else {
- //doesn't display an errror and posts the users first name
- $first_name= $_POST["first_name"];
- }
- if (empty($_POST["last_name"])) {
- //tells the user to input their surname
- $last_nameErr = "Surname is required";
- } else {
- //last name printed
- $last_name = $_POST["last_name"];
- }
- if (empty($_POST["gameID"])) {
- //A game ID must be enterd
- $gameIDErr = "GameID is required";
- } else {
- //post game ID
- $gameID = $_POST["gameID"];
- }
- if (empty($_POST["start_reservation"])) {
- //post reservations error message making the user re enter a value
- $start_reservationErr = "Start reservation date is required";
- } else {
- //post reservations
- $start_reservation = $_POST["start_reservation"];
- }
- if (empty($_POST["num_days"])) {
- //number of days is required
- $num_daysErr = "Number of days is required";
- } else {
- //post number of days
- $num_days = $_POST["num_days"];
- }
- }
- ?>
- <!-- form is set up in php for the users input -->
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- <!-- first name form type is text this allows the user to enter characters an error message will be dispalyed if nothing is inputed -->
- First name: <input name="first_name" type="text" /><span class="error">* <?php echo $first_nameErr;?></span>
- <br><br>
- <!-- surname is the same as firstname -->
- Surname: <input name="last_name" type="text" /><span class="error">* <?php echo $last_nameErr;?></span>
- <br><br>
- <!-- gameid form type is number only allowing intergers to be entered the minimum value inforces the rule that no number less than one can be inputed -->
- GameID: <input name="gameID" type="number" min="1" /><span class="error">* <?php echo $gameIDErr;?></span>
- <br><br>
- <!-- start date reservation has a type of date, thsi displays a calender for the user to chose the date of their reservations. -->
- Start date of reservation: <input name="start_reservation" type="date" /><span class="error">* <?php echo $start_reservationErr;?></span>
- <br><br>
- <!-- the length of the resrvation is again a number and has a minimum value of 1 and a maximum value of 5. This is becasue the length of reservation can only be from 1-5 days, so this stops any other inputs removing the chances of more errors. -->
- Length of reservation: <input name="num_days" type="number" min="1" max="5" /><span class="error">* <?php echo $num_daysErr;?></span>
- <br /><br />
- <!-- this creates the submmit button allowing the users to submit their inputs -->
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- //assign the value of false to error
- $error = false;
- //row equlas zero
- $row = 0;
- while($games[$row][0] != $gameID and $row <= $num_rows) {
- $row++;
- }
- // Checks if the user clicked the submit button
- if ($_POST["submit"] == "Submit") {
- // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
- if ($_POST["num_days"] == "") {
- //change the value of error to false if a value is not entered
- $error = true;
- } elseif($_POST["first_name"] == "") {
- //change the value of error to false if a value is not entered
- $error = true;
- } elseif($_POST["last_name"] == "") {
- //change the value of error to false if a value is not entered
- $error = true;
- } elseif($_POST["gameID"] == "") {
- //change the value of error to false if a value is not entered
- $error = true;
- } elseif($_POST["start_reservation"] == "") {
- //change the value of error to false if a value is not entered
- $error = true;
- // If the gameid doesn't exist it will tell the user
- } elseif($row>$num_rows) {
- $error = true;
- echo "Please enter a valid game ID.";
- } else {
- echo "<h2>Your Inputs:</h2>";
- //prints values entered into the variables
- echo "firstname: ".$first_name;
- echo "<br />";
- echo "Surname ".$last_name;
- echo "<br />";
- echo "GameID: ".$gameID;
- echo "<br />";
- echo "Start date of reservation: ".$start_reservation;
- echo "<br />";
- echo "length of reservation: ".$num_days;
- echo "<br />";
- //calculate the end date of the reservation
- //by adding the number of days to the start date
- $end_date = date('Y-m-d', strtotime($start_reservation. " + $num_days days"));
- echo "End date: ".$end_date;
- echo"<br>";
- //print the game name
- //game name recieved by searching through row 2 in games
- echo "Game Name: ".$games[$row][2];
- //price recuierved from row four where the day to day price exists
- $dayprice = $games[$row][4];
- //price multiplied by numbver of days to get teh total price for teh reservation
- $totalprice = $dayprice * $num_days;
- echo "<br>";
- //add £to the price for aesthetics
- echo "The price for ".$num_days." days is £".$totalprice.".";
- }
- ?>
- <?php
- if ($_POST["submit"] == "Submit") {
- // Checks if there were no errors in the form
- if ($error != true) {
- // Sets the variables for connection details
- $servername = "localhost";
- $username = "yneocleous";
- $password = "ebscs";
- $dbname = "yneocleous";
- // Connects to the database
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Checks if the connection was unsuccessful and if it was, it displays an error
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- } else {
- echo "Connection successful.";
- }
- // Creates the SQL code to check if the game is reserved already
- $checkSQL = "SELECT game_ID
- FROM Reservations
- WHERE (start_reservation between'".$start_reservation."' and '".$end_date."' OR end_date between'".$start_reservation."' and '".$end_date."')
- AND game_ID = '".$gameID."'";
- // Runs the SQL query
- $result = mysqli_query($conn, $checkSQL);
- // Checks if the query returned a result
- if (mysqli_num_rows($result) > 0) {
- // If it did, notfiy the user the game is reserved and do nothing more
- echo "<br><br>Sorry, this game is already reserved during these days. Please try another date.";
- } else {
- //If it did not return anything, then create the SQL query to insert the data into the database
- $sql = "INSERT INTO Reservations (first_name, last_name, game_ID, start_reservation, num_days, end_date)
- VALUES ('".$first_name."', '".$last_name."', '".$gameID."', '".$start_reservation."', '".$num_days."', '".$end_date."')";
- // Check if the query was run successfully
- if ($conn->query($sql)) {
- // If it was, tell the user the reservation was saved.
- echo "<br><br> Reservation Saved.";
- } else {
- // If not, display the error to the user.
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- // Close the connection to prevent any further errors.
- $conn->close();
- }
- }
- }
- }
- }
- ?>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment