Advertisement
Guest User

Untitled

a guest
Apr 21st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. <?php
  2. require("dekey.php");
  3. ?>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  6. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  10. <title>Student Data</title>
  11. </head>
  12.  
  13. <body>
  14. Select a major:<br/>
  15. <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
  16.  
  17. <select name="major">
  18. <option value="">-</option>
  19. <option value="CIS">CIS</option>
  20. <option value="ISI">ISI</option>
  21. </select>
  22. <br /><br />
  23. <input type="submit" name="Submit" value="Submit">
  24. </form>
  25.  
  26. <?php
  27. if(isset($_POST['Submit'])){
  28. main();
  29. }
  30.  
  31. function main(){
  32. $link = connectDB();
  33.  
  34. $major=trim($_POST['major']);
  35. if(verify($major)){
  36. $prepared = queryData($link, $major);
  37. showData($prepared);
  38. }else{
  39. echo "<p>Please select a major</p>";
  40. }
  41. mysqli_close($link);
  42. }
  43.  
  44. function connectDB(){
  45. dekey_main();
  46.  
  47. $host = trim ($GLOBALS['host']);
  48. $username = trim ($GLOBALS['username']);
  49. $password = trim ($GLOBALS['password']);
  50. $db = trim ($GLOBALS['db_name']);
  51.  
  52. $myDB = mysqli_connect($host, $username, $password, $db);
  53.  
  54. if(mysqli_connect_error()){
  55. echo "connection error";
  56. exit;
  57. }
  58. return $myDB;
  59. }
  60.  
  61.  
  62. function verify($major){
  63. $isValid = false;
  64. $major=mb_convert_encoding($major);
  65. $regEx ="^[a-zA-Z]{3,7}$";
  66. if(size(preg_match($regEx, $major)) > 1){
  67. $isValid=true;
  68. }
  69. return $isValid;
  70. }
  71.  
  72.  
  73.  
  74. function queryData($link, $major){
  75. $major=trim($major);
  76.  
  77. if($major=="CIS" || $major=="ISI"){
  78. $studentQuery="SELECT lastname, firstname, major FROM Students";
  79. $prepared=mysqli_prepare($link, $studentQuery);
  80. mysqli_stmt_bind_param($prepared, "s");
  81. $result=mysqli_stmt_execute($prepared);
  82. if($result){
  83. return $prepared;
  84. }
  85. }
  86. return null;
  87. }
  88.  
  89.  
  90. function showData($prepared){
  91. if(!is_null($prepared)){
  92. echo "<table border='0'>";
  93. echo "<tr>";
  94. echo "<td> Last Name</td>";
  95. echo "<td> First Name</td>";
  96. echo "<td> Major</td>";
  97. echo "</tr>";
  98. mysqli_stmt_bind_result ($prepared, $lastName, $firstName, $major);
  99.  
  100. while (mysqli_stmt_fetch($prepared)) {
  101. echo "<tr>";
  102. echo "<td>$lastName</td>";
  103. echo "<td>$firstName</td>";
  104. echo "<td>$major</td>";
  105. echo "</tr>";
  106. }
  107. echo "</table>";
  108. }else{
  109. mysqli_stmt_error($prepared);
  110. }
  111. }
  112. ?>
  113.  
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement