Advertisement
Guest User

Untitled

a guest
May 19th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5.  
  6. <meta charset=”UTF-8">
  7. <title>Week 24 & 25 - SCC130</title>
  8.  
  9. </head>
  10.  
  11.  
  12. <body>
  13.  
  14. <!-- (PART ONE - FIRST NAME, LAST NAME, EMAIL ADDRESS) (PART ONE - FIRST NAME, LAST NAME, EMAIL ADDRESS) -->
  15.  
  16. <form action="home.php" method="post">
  17. First Name : <input type="text" name="first_name" id="first_name" value="<?php if(isset($_POST['first_name'])){echo $_POST['first_name'];} ?>"required><br>
  18. Second Name : <input type="text" name="second_name" id="second_name" value="<?php if(isset($_POST['second_name'])){echo $_POST['second_name'];} ?>" required><br>
  19. Email Address : <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>" required><br>
  20. SCC DB Login : <input type="text" name="login" id="login"><br>
  21. SCC Password : <input type="password" name="password" id="password"><br>
  22. <input type="submit" name = "form 1"><br><br>
  23. </form>
  24.  
  25. <?php
  26. $first_name = $_POST["first_name"];
  27. $second_name = $_POST["second_name"];
  28.  
  29. if (!empty($_POST["first_name"])) {
  30. if (!preg_match("/^[a-zA-Z ]*$/", $first_name)) {
  31. echo "Only letters are allowed for first name.";
  32. }
  33. else if (!empty($_POST["second_name"])) {
  34. if (!preg_match("/^[a-zA-Z ]*$/", $second_name)) {
  35. echo "Only letters are allowed for second name. ";
  36. }else{
  37. echo "Hello " , $first_name, " ", $second_name, ".";
  38. }
  39. }else{
  40. echo "Second name has not been entered. Please enter both names.";
  41. }
  42. }else{
  43. echo "First name has not been entered. Please enter both names.";
  44. }
  45. ?><br><br>
  46.  
  47. <?php
  48. $email = $_POST["email"];
  49. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  50. echo "Email isn't in an email format. (May contain illegal characters)";
  51. }else{
  52. if (!empty($_POST["email"])) {
  53. echo "Your email is: " , $email;
  54. }else{
  55. echo "Email has not been entered. Please enter second name.";
  56. }
  57. }
  58. ?><br><br>
  59.  
  60. <!-- (PART ONE - FIRST NAME, LAST NAME, EMAIL ADDRESS) (PART ONE - FIRST NAME, LAST NAME, EMAIL ADDRESS) -->
  61.  
  62.  
  63. <!-- (PART TWO ) -->
  64.  
  65. <?php
  66.  
  67. if(empty ($_POST["login"]) && ($_POST["password"])){
  68.  
  69. echo "Please enter both Username and Password";
  70.  
  71. }
  72.  
  73. $username = $_POST["login"];
  74. $password = $_POST["password"];
  75.  
  76. $DB_USER = $username;
  77. $DB_PASSWORD = $password;
  78. $DB_HOST = 'localhost';
  79. $DB_NAME = 'scc_130';
  80.  
  81. try{
  82. $db=new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=utf8", $DB_USER, $DB_PASSWORD); //connect to the database
  83. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //throw exceptions when an error occurs
  84. }catch(PDOException $e){
  85. echo "Your Username and Password is incorrect.";
  86. }
  87. ?>
  88.  
  89. <!-- (PART TWO ) -->
  90.  
  91. <!-- (PART THREE ) -->
  92. <p>
  93. What table would you like to view?
  94. <form action="home.php" method = "post">
  95. <select name="table" onchange="this.form.submit()">
  96. <option value="">Select...</option>
  97. <option value="Company">Company</option>
  98. <option value="Fruits">Fruits</option>
  99. <option value="Population">Population</option>
  100. <br><br>
  101. </select>
  102.  
  103. </p>
  104.  
  105. <?php
  106. $tableSelect = $_POST["table"];
  107. if(strcmp($tableSelect, 'Company') == 0){
  108. require_once('connect.php');
  109. try{
  110. $query = 'SELECT * FROM company';
  111. $statement = $db->query($query);
  112. while($row = $statement->fetch(PDO::FETCH_ASSOC)){
  113. echo $row['Year'] . ' , ';
  114. echo '&nbsp';
  115. echo $row['Sales'];
  116. echo '&nbsp';
  117. echo $row['Expenses'];
  118. echo '<br>';
  119. echo '<br>';
  120. }
  121. }catch(PDOException $e){
  122. $message = '<p>Something went wrong!</p><p>' . $e->getMessage(). '</p>';
  123. }
  124. }
  125. else if(strcmp($tableSelect, 'Fruits') == 0){
  126. require_once('connect.php');
  127. try{
  128. $query = 'SELECT * FROM fruits';
  129. $statement = $db->query($query);
  130. while($row = $statement->fetch(PDO::FETCH_ASSOC)){
  131. echo $row['fruit_name'] . ' , ';
  132. echo '&nbsp';
  133. echo $row['amount'];
  134. echo '<br>';
  135. echo '<br>';
  136. }
  137. }catch(PDOException $e){
  138. $message = '<p>Something went wrong!</p><p>' . $e->getMessage(). '</p>';
  139. }
  140. }
  141. else if(strcmp($tableSelect, 'Population') == 0){
  142. require_once('connect.php');
  143. try{
  144. $query = 'SELECT * FROM population';
  145. $statement = $db->query($query);
  146. while($row = $statement->fetch(PDO::FETCH_ASSOC)){
  147. echo $row['City'] . ' , ';
  148. echo '&nbsp';
  149. echo $row['People'];
  150. echo '<br>';
  151. echo '<br>';
  152. }
  153. }catch(PDOException $e){
  154. $message = '<p>Something went wrong!</p><p>' . $e->getMessage(). '</p>';
  155. }
  156. }
  157.  
  158.  
  159. ?>
  160.  
  161. <!-- (PART THREE ) -->
  162.  
  163. </body>
  164.  
  165.  
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement