Advertisement
Guest User

Untitled

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