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>
- <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</h1>
- <?php
- // Creates variables
- $name = $gameID = $startDate = $NoOfDays = "";
- // Creates error variables
- $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr = "";
- // Checks if the request method is post.
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- // Checks if the user did not enter anything for their name
- if (empty($_POST["name"])) {
- // Creates the error
- $nameErr = "Name is required.";
- } else {
- // Reads in the value for the name
- $name = $_POST["name"];
- }
- // Checks if the user did not enter anything for their gameID
- if (empty($_POST["gameID"])) {
- // Creates the error
- $gameIDErr = "The game ID is required";
- } else {
- // Reads in the value for the gameID
- $gameID = $_POST["gameID"];
- }
- // Checks if the user did not enter anything for the date
- if (empty($_POST["startDate"])) {
- // Creates the error
- $startDateErr = "The start date is required";
- } else {
- // Reads in the value for the start date.
- $startDate = $_POST["startDate"];
- }
- // Checks if the user did not enter anything for the number of days
- if (empty($_POST["NoOfDays"])) {
- // Creates the error
- $NoOfDaysErr = "The number of days is required.";
- } else {
- // Reads in the value for the number of days.
- $NoOfDays = $_POST["NoOfDays"];
- }
- }
- // Opens the file
- $file = fopen("games.csv","r");
- // Loops while it is not at the end of the file
- while(! feof($file)){
- // Reads in everything from the file and stores it in a multidimensional array
- $gameDetails[] = fgetcsv($file);
- }
- // Calculates the number of rows in the array
- $num_rows = count($gameDetails);
- // Eliminates the error in which it displays that there is an extra row
- $num_rows = $num_rows - 1;
- // Closes the file to avoid any errors
- fclose($file)
- ?>
- <!-- sets the colour of the error message to red -->
- <style>
- .error {color: #FF0000;}
- </style>
- <!-- sets the key that is displayed at the top of the page-->
- <p><span class="error">* required field.</span></p>
- <!-- sets the method to post and the action so that it can display on the same page-->
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- <!-- creates the form for the name-->
- Name: <input type="text" name="name">
- <!-- creates the error for the name -->
- <span class="error">*<?php echo $nameErr;?></span>
- <br><br>
- <!-- creates the form for the gameID-->
- Game ID: <input type="number" name="gameID" min="1">
- <!-- creates the error for the gameID-->
- <span class="error">*<?php echo $gameIDErr;?></span>
- <br><br>
- <!-- creates the form for the start date-->
- Start Date: <input type="date" name="startDate">
- <!-- creates the error for the start date-->
- <span class="error">*<?php echo $startDateErr;?></span>
- <br><br>
- <!-- creates the from for the number of days-->
- Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" <id="NoOfDays">
- <!-- creates the error for the number of days-->
- <span class="error">*<?php echo $NoOfDaysErr;?></span>
- <br><br>
- <!-- creates the submit button-->
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- // Creates the error boolean
- $error = false;
- // Creates the value that will be increased in the while loop
- $row = 0;
- // Creates the while loop that loops until the gameID entered is found in the table.
- while($gameDetails[$row][0] != $gameID and $row <= $num_rows) {
- $row++;
- }
- $servername = "localhost";
- $username = "wessamallami";
- $password = "circle8";
- $dbname = "wallami";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $checkSQL = "SELECT GameID
- FROM Reservations
- WHERE (Start_Date BETWEEN '".$startDate."' AND '".$endDate."' OR End_Date BETWEEN '".$startDate."' AND '".$endDate."') AND GameId == '".$gameID."'";
- $result = mysql_query($checkSQL);
- echo "Game ID: " . $gameID;
- echo "<br>";
- $conn->close();
- // 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["NoOfDays"] == "") {
- $error = true;
- } elseif($_POST["name"] == "") {
- $error = true;
- } elseif($_POST["gameID"] == "") {
- $error = true;
- } elseif($_POST["startDate"] == "") {
- $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.";
- } elseif (!$result) {
- echo "Sorry, this game is already reserved during these days.";
- } else {
- $error = false;
- // Displays the entered values
- echo "<h2>Values:</h2>";
- echo "Name: ".$name;
- echo "<br>";
- echo "Game ID: ".$gameID;
- echo "<br>";
- echo "Start Date: ".$startDate;
- echo "<br>";
- echo "Number Of Days: ".$NoOfDays;
- echo "<br>";
- $endDate = date('Y-m-d', strtotime($startDate. " + $NoOfDays days"));
- echo "End Date: ". $endDate;
- echo "<br>";
- // Displays the game name from the ID that was found in the table
- echo "Game Name: ".$gameDetails[$row][2];
- // Calculates the price of the game for one day
- $dayprice = $gameDetails[$row][4];
- // Calculates the total price
- $totalprice = $dayprice * $NoOfDays;
- echo "<br>";
- // Tells the user the price for the number of days they entered
- echo "The price for ".$NoOfDays." days is £".$totalprice.".";
- }
- }
- ?>
- <?php
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if ($error != true) {
- //connect to mysql database
- $servername = "localhost";
- $username = "wessamallami";
- $password = "circle8";
- $dbname = "wallami";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
- VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
- if ($conn->multi_query($sql)) {
- echo "<br><br> Reservation Saved.";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- }
- }
- ?>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment