Guest User

asdf

a guest
Mar 2nd, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 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. <div id='main'>
  11. <div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
  12. <div id='menu-wrapper'>
  13. <div id='menu'>
  14. <ul>
  15.  
  16. <li><a href='index.php'>Home</a></li>
  17. <li class='current_page_item'><a href='#'>Reservations</a></li>
  18. </ul>
  19. </div>
  20. </div>
  21.  
  22. <div>
  23. <h1>Reservations</h1>
  24. <?php
  25. // Creates variables
  26. $name = $gameID = $startDate = $NoOfDays = "";
  27. // Creates error variables
  28. $nameErr = $gameIDErr = $startDateErr = $NoOfDaysErr = "";
  29. // Checks if the request method is post.
  30. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  31. // Checks if the user did not enter anything for their name
  32. if (empty($_POST["name"])) {
  33. // Creates the error
  34. $nameErr = "Name is required.";
  35. } else {
  36. // Reads in the value for the name
  37. $name = $_POST["name"];
  38. }
  39. // Checks if the user did not enter anything for their gameID
  40. if (empty($_POST["gameID"])) {
  41. // Creates the error
  42. $gameIDErr = "The game ID is required";
  43. } else {
  44. // Reads in the value for the gameID
  45. $gameID = $_POST["gameID"];
  46. }
  47. // Checks if the user did not enter anything for the date
  48. if (empty($_POST["startDate"])) {
  49. // Creates the error
  50. $startDateErr = "The start date is required";
  51. } else {
  52. // Reads in the value for the start date.
  53. $startDate = $_POST["startDate"];
  54. }
  55. // Checks if the user did not enter anything for the number of days
  56. if (empty($_POST["NoOfDays"])) {
  57. // Creates the error
  58. $NoOfDaysErr = "The number of days is required.";
  59. } else {
  60. // Reads in the value for the number of days.
  61. $NoOfDays = $_POST["NoOfDays"];
  62. }
  63. }
  64. // Opens the file
  65. $file = fopen("games.csv","r");
  66. // Loops while it is not at the end of the file
  67. while(! feof($file)){
  68. // Reads in everything from the file and stores it in a multidimensional array
  69. $gameDetails[] = fgetcsv($file);
  70. }
  71. // Calculates the number of rows in the array
  72. $num_rows = count($gameDetails);
  73. // Eliminates the error in which it displays that there is an extra row
  74. $num_rows = $num_rows - 1;
  75. // Closes the file to avoid any errors
  76. fclose($file)
  77. ?>
  78. <!-- sets the colour of the error message to red -->
  79. <style>
  80. .error {color: #FF0000;}
  81. </style>
  82. <!-- sets the key that is displayed at the top of the page-->
  83. <p><span class="error">* required field.</span></p>
  84. <!-- sets the method to post and the action so that it can display on the same page-->
  85. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  86. <!-- creates the form for the name-->
  87. Name: <input type="text" name="name">
  88. <!-- creates the error for the name -->
  89. <span class="error">*<?php echo $nameErr;?></span>
  90. <br><br>
  91. <!-- creates the form for the gameID-->
  92. Game ID: <input type="number" name="gameID" min="1">
  93. <!-- creates the error for the gameID-->
  94. <span class="error">*<?php echo $gameIDErr;?></span>
  95. <br><br>
  96. <!-- creates the form for the start date-->
  97. Start Date: <input type="date" name="startDate">
  98. <!-- creates the error for the start date-->
  99. <span class="error">*<?php echo $startDateErr;?></span>
  100. <br><br>
  101. <!-- creates the from for the number of days-->
  102. Number Of Days: <input type="number" name="NoOfDays" min="1" max="5" <id="NoOfDays">
  103. <!-- creates the error for the number of days-->
  104. <span class="error">*<?php echo $NoOfDaysErr;?></span>
  105. <br><br>
  106. <!-- creates the submit button-->
  107. <input type="submit" name="submit" value="Submit">
  108. </form>
  109. <?php
  110. // Creates the error boolean
  111. $error = false;
  112. // Creates the value that will be increased in the while loop
  113. $row = 0;
  114. // Creates the while loop that loops until the gameID entered is found in the table.
  115. while($gameDetails[$row][0] != $gameID and $row <= $num_rows) {
  116. $row++;
  117. }
  118. $servername = "localhost";
  119. $username = "wessamallami";
  120. $password = "circle8";
  121. $dbname = "wallami";
  122. $conn = new mysqli($servername, $username, $password, $dbname);
  123. if ($conn->connect_error) {
  124. die("Connection failed: " . $conn->connect_error);
  125. }
  126. $checkSQL = "SELECT GameID
  127. FROM Reservations
  128. WHERE (Start_Date BETWEEN '".$startDate."' AND '".$endDate."' OR End_Date BETWEEN '".$startDate."' AND '".$endDate."') AND GameId == '".$gameID."'";
  129. $result = mysql_query($checkSQL);
  130. echo "Game ID: " . $gameID;
  131. echo "<br>";
  132. $conn->close();
  133. // Checks if the user clicked the submit button
  134. if ($_POST["submit"] == "Submit") {
  135. // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
  136. if ($_POST["NoOfDays"] == "") {
  137. $error = true;
  138. } elseif($_POST["name"] == "") {
  139. $error = true;
  140. } elseif($_POST["gameID"] == "") {
  141. $error = true;
  142. } elseif($_POST["startDate"] == "") {
  143. $error = true;
  144. // If the gameid doesn't exist it will tell the user
  145. } elseif($row>$num_rows) {
  146. $error = true;
  147. echo "Please enter a valid game ID.";
  148. } elseif (!$result) {
  149. echo "Sorry, this game is already reserved during these days.";
  150. } else {
  151. $error = false;
  152. // Displays the entered values
  153. echo "<h2>Values:</h2>";
  154. echo "Name: ".$name;
  155. echo "<br>";
  156. echo "Game ID: ".$gameID;
  157. echo "<br>";
  158. echo "Start Date: ".$startDate;
  159. echo "<br>";
  160. echo "Number Of Days: ".$NoOfDays;
  161. echo "<br>";
  162. $endDate = date('Y-m-d', strtotime($startDate. " + $NoOfDays days"));
  163. echo "End Date: ". $endDate;
  164. echo "<br>";
  165. // Displays the game name from the ID that was found in the table
  166. echo "Game Name: ".$gameDetails[$row][2];
  167. // Calculates the price of the game for one day
  168. $dayprice = $gameDetails[$row][4];
  169. // Calculates the total price
  170. $totalprice = $dayprice * $NoOfDays;
  171. echo "<br>";
  172. // Tells the user the price for the number of days they entered
  173. echo "The price for ".$NoOfDays." days is £".$totalprice.".";
  174. }
  175. }
  176. ?>
  177.  
  178.  
  179.  
  180.  
  181. <?php
  182. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  183. if ($error != true) {
  184. //connect to mysql database
  185. $servername = "localhost";
  186. $username = "wessamallami";
  187. $password = "circle8";
  188. $dbname = "wallami";
  189. $conn = new mysqli($servername, $username, $password, $dbname);
  190. if ($conn->connect_error) {
  191. die("Connection failed: " . $conn->connect_error);
  192. }
  193. $sql = "INSERT INTO Reservations (Customer_Name, Game_ID, Start_Date, End_Date, Number_Of_Days_Reserved)
  194. VALUES ('".$name."', '".$gameID."', '".$startDate."', '".$endDate."', '".$NoOfDays."')";
  195. if ($conn->multi_query($sql)) {
  196. echo "<br><br> Reservation Saved.";
  197. } else {
  198. echo "Error: " . $sql . "<br>" . $conn->error;
  199. }
  200. $conn->close();
  201. }
  202. }
  203. ?>
  204.  
  205.  
  206.  
  207. </div>
  208.  
  209. </div>
  210.  
  211. </body>
  212. </html>
Add Comment
Please, Sign In to add comment