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">
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <div id='main'>
- <div id='titleImage'><img title='Home' src='GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
- <div id='menu-wrapper'>
- <div id='menu'>
- <ul>
- <li><a href='IndexR.php'>Home</a></li>
- <li class='current_page_item'><a href='#'>Reservations</a></li>
- </ul>
- </div>
- </div>
- <div>
- <h1>Reservations</h1>
- </div>
- <?php
- // Define variables and set to empty values.
- $gameID = $startDate = $name = $days = "";
- $gameerr = $startdateerr = $nameerr = $dayserr = $submit = $submitSearch = $searchResult = "";
- $validation = 0;
- // Saves contents of the game file.
- $file = fopen("games.csv","r");
- if(fopen("games.csv","r")) {
- while(! feof($file)){
- $gameinfo[] = fgetcsv($file);
- }
- }
- fclose($file);
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (isset($_POST['searchButton'])) {
- // Checks if there is no user input and displays an error message if there isn't.
- if (empty($_POST["search"])) {
- $submitSearch = "No input to search for.";
- } else {
- // Converts string to lowercase so that the search isn't case sensitive.
- $keyword = strtolower($_POST["search"]);
- $rowsNumber = count($gameinfo) - 1;
- // Searches through the game details to find a game that matched with the keyword.
- for($row = 0; $row < $rowsNumber; $row++) {
- $currentName = strtolower($gameinfo[$row][2]);
- $currentDesc = strtolower($gameinfo[$row][3]);
- if (strpos($currentName, $keyword) !== false) {
- $searchResult = $gameinfo[$row][2];
- $searchFound = 1;
- } else {
- if (strpos($currentDesc, $keyword) !== false) {
- $searchResult = $gameinfo[$row][2];
- $searchFound = 1;
- }
- }
- if ($searchFound == 1) {
- // Displays the results of the search.
- $LD = $row + 1;
- $submitSearch = $submitSearch." <br><a href=http://tinyurl.com/jkkdzzy?GID=".$LD.">".$searchResult."</a><br>";
- $searchFound = 0;
- }
- }
- }
- }
- }
- // Store contents of form in variables.
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (isset($_POST['submit'])) {
- // Checks if there is no user input and displays an error message if there isn't.
- if (empty($_POST["gameID"])) {
- $gameerr = "Game ID is required.";
- $validation = 0;
- } else {
- // Saves the raw ID of the chosen game.
- $ID = $_POST["gameID"];
- // Goes through all of the games in the file until it finds the matching index.
- $found = 0;
- $rowsNumber = count($gameinfo) - 1;
- for($row = 0; $row < $rowsNumber; $row++) {
- // If the game index is found, the search stops and the value is stored.
- if ($gameinfo[$row][0] == $_POST["gameID"]) {
- $gameID = $gameinfo[$row][2];// Stores the name of the game corresponding to the inputted game ID.
- $found = 1;
- $validation = $validation + 1;
- $row = $rowsNumber;
- }
- }
- // If the game index is not found, then an error message is displayed.
- if ($found == 0) {
- $gameerr = "Game ID doesn't exist.";
- $validation = 0;
- }
- }
- if (empty($_POST["name"])) {
- $nameerr = "Your name is required.";
- $validation = 0;
- } else {
- $name = $_POST["name"];
- $validation = $validation + 1;
- }
- if (empty($_POST["days"])) {
- $dayserr = "Amount of days is required.";
- $validation = 0;
- } else {
- // Checks if the number of days inputted exceeds the number 5.
- if ($_POST["days"] > 5) {
- $dayserr = "5 is the maximum number of days you can rent.";
- $validation = 0;
- } else {
- $days = $_POST["days"];
- $validation = $validation + 1;
- }
- }
- if (empty($_POST["startDate"])) {
- $startdateerr = "Start date of reservation is required.";
- $validation = 0;
- } else {
- $currentDate = date('Y-m-d');
- // Checks if the inputted date is in the past, making it invalid.
- if ($_POST["startDate"] > $currentDate) {
- $startDate = $_POST["startDate"];// Saves the start date.
- $endDate = date('Y-m-d', strtotime($startDate. ' + '.$days. ' days'));// Saves the end date.
- if ($validation == 3) {
- $validation = 0;
- // Mysql database.
- $servername = "fdb5.awardspace.net";
- $username = "1967104_general";
- $password = "ebscomputing1";
- $dbname = "1967104_general";
- // Creates a connection
- $conn = new MySQLi($servername, $username, $password, $dbname);
- // Checks the connection.
- if ($conn->connect_error) { // Displays an error if it can't connect.
- die("Connection failed: " . $conn->connect_error);
- }
- // Checks if the dates overlap.
- $overlap = "SELECT ID
- FROM ReservationTable
- WHERE (start BETWEEN '".$startDate."' AND '".$endDate."' OR Enddate BETWEEN '".$startDate."' AND '".$endDate."')
- AND ID = '".$ID."'";
- $result = mysqli_query($conn, $overlap);
- if (mysqli_num_rows($result) > 0) {
- $submit = "Please pick another date.";
- } else {
- //create SQL INSERT INTO query
- $sql = "INSERT INTO ReservationTable (ID, gameID, start, Enddate, reserve)
- VALUES ('".$ID."', '".$gameID."', '".$startDate."', '".$endDate."', '".$days."')";
- if ($conn->query($sql) === TRUE) { // Sends the query to the database table and checks the results.
- // Displays the total cost of your reservation.
- $submit = "Reservation saved. Total cost: £".$gameinfo[$ID][4] * $days;
- } else {
- $submit = "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();// Disconnects from the database.
- }
- }
- } else {
- $validation = 0;
- $startdateerr = "Please use a valid date.";
- }
- }
- }
- }
- ?>
- <!-- A form used to search for a game. -->
- <form method="post" action"<?php echo htmlspecialchars($_SEARCH["PHP_SELF"]);?>">
- Search: <input type ="text" name="search" />
- <br /><br />
- <input type="submit" name="searchButton" value="Search" />
- <span class="error"> <?php echo $submitSearch;?></span>
- <br /><br />
- <br /><br />
- </form>
- <span class="error">* required field.</span>
- <!-- A form that takes in the user input and the values are sent for processing. -->
- <form method="post" action"<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Game ID: <input type="text" name="gameID" value = "<?php echo $_GET['GID']?>"/>
- <span class="error">* <?php echo $gameerr;?></span>
- <br /><br />
- Name: <input type="text" name="name" />
- <span class="error">* <?php echo $nameerr;?></span>
- <br /><br />
- Start date of the reservation: <input type="date" name="startDate" />
- <span class="error">* <?php echo $startdateerr;?></span>
- <br /><br />
- Amount of reserved days: <input type="text" name="days" />
- <span class="error">* <?php echo $dayserr;?></span>
- <br /><br />
- <input type="submit" name="submit" value="Reserve">
- <span class="error"> <?php echo $submit;?></span>
- <br /><br />
- </form>
- <?php
- // Display contents of form
- echo "<h2>Your Input:</h2>";
- echo $gameID;
- echo "<br>";
- echo $name;
- echo "<br>";
- echo $startDate;
- echo "<br>";
- echo $days;
- ?>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment