Guest User

Untitled

a guest
Mar 21st, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Games Library Reservations Page</title>
  6. <link href="styles.css" rel="stylesheet" type="text/css">
  7. </head>
  8.  
  9. <body>
  10. <style>
  11. .error {color: #FF0000;}
  12. </style>
  13. <div id='main'>
  14. <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
  15. <div id='menu-wrapper'>
  16. <div id='menu'>
  17. <ul>
  18.  
  19. <li><a href='index.php'>Home</a></li>
  20. <li class='current_page_item'><a href='#'>Reservations</a></li>
  21. </ul>
  22. </div>
  23. </div>
  24.  
  25. <div>
  26. <h1>Reservations form</h1>
  27. <h4> Welcome to the reservation page here you can reserve your choice of game</h4>
  28. </div>
  29. <?php
  30. $file = fopen("games.csv","r");
  31.  
  32. //stores it in games
  33. if(fopen("games.csv","r")) {
  34. while(! feof($file)){
  35. //puts games into an array
  36. $games[] = fgetcsv($file);
  37. }
  38. //count the amount of rows
  39. $num_rows = count($games);
  40.  
  41. //create the variables for the input values and give it a value of nothing
  42. $first_name = $last_name = $gameID = $start_reservation = $num_days = "";
  43. //create variabls to store the message that will display when an error is met
  44. $first_nameErr = $last_nameErr = $gameIDErr = $start_reservationErr = $num_daysErr = "";
  45. //error catching- this part checks to see if a value hasd been inputed for all the variables
  46. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  47. if (empty($_POST["first_name"])) {
  48. //displays an error message
  49. $first_nameErr = "first name is required";
  50. } else {
  51. //doesn't display an errror and posts the users first name
  52. $first_name= $_POST["first_name"];
  53. }
  54. if (empty($_POST["last_name"])) {
  55. //tells the user to input their surname
  56. $last_nameErr = "Surname is required";
  57. } else {
  58. //last name printed
  59. $last_name = $_POST["last_name"];
  60. }
  61. if (empty($_POST["gameID"])) {
  62. //A game ID must be enterd
  63. $gameIDErr = "GameID is required";
  64. } else {
  65. //post game ID
  66. $gameID = $_POST["gameID"];
  67. }
  68. if (empty($_POST["start_reservation"])) {
  69. //post reservations error message making the user re enter a value
  70. $start_reservationErr = "Start reservation date is required";
  71. } else {
  72. //post reservations
  73. $start_reservation = $_POST["start_reservation"];
  74. }
  75. if (empty($_POST["num_days"])) {
  76. //number of days is required
  77. $num_daysErr = "Number of days is required";
  78. } else {
  79. //post number of days
  80. $num_days = $_POST["num_days"];
  81. }
  82. }
  83. ?>
  84.  
  85. <!-- form is set up in php for the users input -->
  86. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  87. <!-- first name form type is text this allows the user to enter characters an error message will be dispalyed if nothing is inputed -->
  88. First name: <input name="first_name" type="text" /><span class="error">* <?php echo $first_nameErr;?></span>
  89. <br><br>
  90. <!-- surname is the same as firstname -->
  91. Surname: <input name="last_name" type="text" /><span class="error">* <?php echo $last_nameErr;?></span>
  92. <br><br>
  93. <!-- 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 -->
  94. GameID: <input name="gameID" type="number" min="1" /><span class="error">* <?php echo $gameIDErr;?></span>
  95. <br><br>
  96. <!-- start date reservation has a type of date, thsi displays a calender for the user to chose the date of their reservations. -->
  97. Start date of reservation: <input name="start_reservation" type="date" /><span class="error">* <?php echo $start_reservationErr;?></span>
  98. <br><br>
  99. <!-- 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. -->
  100. Length of reservation: <input name="num_days" type="number" min="1" max="5" /><span class="error">* <?php echo $num_daysErr;?></span>
  101. <br /><br />
  102. <!-- this creates the submmit button allowing the users to submit their inputs -->
  103. <input type="submit" name="submit" value="Submit">
  104. </form>
  105. <?php
  106. //assign the value of false to error
  107. $error = false;
  108. //row equlas zero
  109. $row = 0;
  110. while($games[$row][0] != $gameID and $row <= $num_rows) {
  111. $row++;
  112. }
  113.  
  114.  
  115. // Checks if the user clicked the submit button
  116. if ($_POST["submit"] == "Submit") {
  117. // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
  118. if ($_POST["num_days"] == "") {
  119. //change the value of error to false if a value is not entered
  120. $error = true;
  121. } elseif($_POST["first_name"] == "") {
  122. //change the value of error to false if a value is not entered
  123. $error = true;
  124. } elseif($_POST["last_name"] == "") {
  125. //change the value of error to false if a value is not entered
  126. $error = true;
  127. } elseif($_POST["gameID"] == "") {
  128. //change the value of error to false if a value is not entered
  129. $error = true;
  130. } elseif($_POST["start_reservation"] == "") {
  131. //change the value of error to false if a value is not entered
  132. $error = true;
  133. // If the gameid doesn't exist it will tell the user
  134. } elseif($row>$num_rows) {
  135. $error = true;
  136. echo "Please enter a valid game ID.";
  137. } else {
  138.  
  139.  
  140. echo "<h2>Your Inputs:</h2>";
  141. //prints values entered into the variables
  142. echo "firstname: ".$first_name;
  143. echo "<br />";
  144. echo "Surname ".$last_name;
  145. echo "<br />";
  146. echo "GameID: ".$gameID;
  147. echo "<br />";
  148. echo "Start date of reservation: ".$start_reservation;
  149. echo "<br />";
  150. echo "length of reservation: ".$num_days;
  151. echo "<br />";
  152. //calculate the end date of the reservation
  153. //by adding the number of days to the start date
  154. $end_date = date('Y-m-d', strtotime($start_reservation. " + $num_days days"));
  155. echo "End date: ".$end_date;
  156. echo"<br>";
  157. //print the game name
  158. //game name recieved by searching through row 2 in games
  159. echo "Game Name: ".$games[$row][2];
  160. //price recuierved from row four where the day to day price exists
  161. $dayprice = $games[$row][4];
  162. //price multiplied by numbver of days to get teh total price for teh reservation
  163. $totalprice = $dayprice * $num_days;
  164. echo "<br>";
  165. //add £to the price for aesthetics
  166. echo "The price for ".$num_days." days is £".$totalprice.".";
  167. }
  168.  
  169.  
  170. ?>
  171. <?php
  172. if ($_POST["submit"] == "Submit") {
  173. // Checks if there were no errors in the form
  174. if ($error != true) {
  175. // Sets the variables for connection details
  176. $servername = "localhost";
  177. $username = "yneocleous";
  178. $password = "ebscs";
  179. $dbname = "yneocleous";
  180. // Connects to the database
  181. $conn = mysqli_connect($servername, $username, $password, $dbname);
  182. // Checks if the connection was unsuccessful and if it was, it displays an error
  183. if (!$conn) {
  184. die("Connection failed: " . mysqli_connect_error());
  185. } else {
  186. echo "Connection successful.";
  187. }
  188. // Creates the SQL code to check if the game is reserved already
  189. $checkSQL = "SELECT game_ID
  190. FROM Reservations
  191. WHERE (start_reservation between'".$start_reservation."' and '".$end_date."' OR end_date between'".$start_reservation."' and '".$end_date."')
  192. AND game_ID = '".$gameID."'";
  193. // Runs the SQL query
  194. $result = mysqli_query($conn, $checkSQL);
  195. // Checks if the query returned a result
  196. if (mysqli_num_rows($result) > 0) {
  197. // If it did, notfiy the user the game is reserved and do nothing more
  198. echo "<br><br>Sorry, this game is already reserved during these days. Please try another date.";
  199. } else {
  200. //If it did not return anything, then create the SQL query to insert the data into the database
  201. $sql = "INSERT INTO Reservations (first_name, last_name, game_ID, start_reservation, num_days, end_date)
  202. VALUES ('".$first_name."', '".$last_name."', '".$gameID."', '".$start_reservation."', '".$num_days."', '".$end_date."')";
  203. // Check if the query was run successfully
  204. if ($conn->query($sql)) {
  205. // If it was, tell the user the reservation was saved.
  206. echo "<br><br> Reservation Saved.";
  207. } else {
  208. // If not, display the error to the user.
  209. echo "Error: " . $sql . "<br>" . $conn->error;
  210. }
  211. // Close the connection to prevent any further errors.
  212. $conn->close();
  213. }
  214. }
  215. }
  216. }
  217. }
  218.  
  219. ?>
  220. </div>
  221. </body>
  222. </html>
Add Comment
Please, Sign In to add comment