Guest User

Untitled

a guest
Mar 4th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 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</h1>
  27. </div>
  28.  
  29.  
  30. <?php
  31. $file = fopen("games.csv","r");
  32.  
  33. //stores it in games
  34. if(fopen("games.csv","r")) {
  35. while(! feof($file)){
  36. $games[] = fgetcsv($file);
  37. }
  38. $num_rows = count($games);
  39.  
  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
  52. $first_name= $_POST["first_name"];
  53. }
  54. if (empty($_POST["last_name"])) {
  55. $last_nameErr = "Surname is required";
  56. } else {
  57. $last_name = $_POST["last_name"];
  58. }
  59. if (empty($_POST["gameID"])) {
  60. $gameIDErr = "GameID is required";
  61. } else {
  62. $gameID = $_POST["gameID"];
  63. }
  64. if (empty($_POST["start_reservation"])) {
  65. $start_reservationErr = "Start reservation date is required";
  66. } else {
  67. $start_reservation = $_POST["start_reservation"];
  68. }
  69. if (empty($_POST["num_days"])) {
  70. $num_daysErr = "Number of days is required";
  71. } else {
  72. $num_days = $_POST["num_days"];
  73. }
  74. }
  75. }
  76. ?>
  77.  
  78. <!-- form is set up in php for the users input -->
  79. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  80. <!-- first name form type is text this allows the user to enter characters an error message will be dispalyed if nothing is inputed -->
  81. First name: <input name="first_name" type="text" /><span class="error">* <?php echo $first_nameErr;?></span>
  82. <br><br>
  83. <!-- surname is the same as firstname -->
  84. Surname: <input name="last_name" type="text" /><span class="error">* <?php echo $last_nameErr;?></span>
  85. <br><br>
  86. <!-- 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 -->
  87. GameID: <input name="gameID" type="number" min="1" /><span class="error">* <?php echo $gameIDErr;?></span>
  88. <br><br>
  89. <!-- start date reservation has a type of date, thsi displays a calender for the user to chose the date of their reservations. -->
  90. Start date of reservation: <input name="start_reservation" type="date" /><span class="error">* <?php echo $start_reservationErr;?></span>
  91. <br><br>
  92. <!-- 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. -->
  93. Length of reservation: <input name="num_days" type="number" min="1" max="5" /><span class="error">* <?php echo $num_daysErr;?></span>
  94. <br /><br />
  95. <!-- this creates the submmit button allowing the users to submit their inputs -->
  96. <input type="submit" name="submit" value="Submit">
  97. </form>
  98. <?php
  99. $error = false;
  100. $row = 0;
  101. while($games[$row][0] != $gameID and $row <= $num_rows) {
  102. $row++;
  103. }
  104.  
  105.  
  106. // Checks if the user clicked the submit button
  107. if ($_POST["submit"] == "Submit") {
  108. // Checks if the user did not enter anything in one of the forms, and if they didn't, it will set error to true.
  109. if ($_POST["num_days"] == "") {
  110. $error = true;
  111. } elseif($_POST["first_name"] == "") {
  112. $error = true;
  113. } elseif($_POST["last_name"] == "") {
  114. $error = true;
  115. } elseif($_POST["gameID"] == "") {
  116. $error = true;
  117. } elseif($_POST["start_reservation"] == "") {
  118. $error = true;
  119. // If the gameid doesn't exist it will tell the user
  120. } elseif($row>$num_rows) {
  121. $error = true;
  122. echo "Please enter a valid game ID.";
  123. } else {
  124.  
  125.  
  126. echo "<h2>Your Inputs:</h2>";
  127. //prints values entered into the variables
  128. echo $first_name;
  129. echo "<br />";
  130. echo $last_name;
  131. echo "<br />";
  132. echo $gameID;
  133. echo "<br />";
  134. echo $start_reservation;
  135. echo "<br />";
  136. echo $num_days;
  137. echo "<br />";
  138. $end_date = date('Y-m-d', strtotime($start_reservation. " + $num_days days"));
  139. echo "End date: ".$end_date;
  140. echo"<br>";
  141. echo "Game Name: ".$games[$row][2];
  142. $dayprice = $games[$row][4];
  143. $totalprice = $dayprice * $num_days;
  144. echo "<br>";
  145. echo "The price for ".$num_days." days is £".$totalprice.".";
  146. }
  147. }
  148. ?>
  149. <?php
  150. //connect to mysql database
  151. $servername = "localhost";
  152. $username = "yneocleous";
  153. $password = "ebscs";
  154. $dbname = "yneocleous";
  155. //create connection
  156. $conn = new MySQLi($servername, $username, $password, $dbname);
  157. //check connection
  158. if ($conn->connect_error) {//if error stop loading html and display error
  159. die("Connection failed: " . $conn->connect_error);
  160. }
  161. //insert a form data as a new record
  162. $sql = "INSERT INTO Reservations (first_name, last_name, game_ID, start_reservation, num_days, end_date)
  163. VALUES ('".$first_name."', '".$last_name."', '".$gameID."', '".$start_reservation."', '".$num_days."', '".$end_date."')";
  164.  
  165. if($conn->query($sql) === TRUE ) {
  166. echo "<br>New record successfully created";
  167. } else {
  168. echo "Error: ". $sql . "<br>" .$conn->error;
  169. }
  170.  
  171. $conn->close();
  172. ?>
  173. </div>
  174. </body>
  175. </html>
Add Comment
Please, Sign In to add comment