Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require("dekey.php");
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
- <title>Student Data</title>
- </head>
- <body>
- Select a major:<br/>
- <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
- <select name="major">
- <option value="">-</option>
- <option value="CIS">CIS</option>
- <option value="ISI">ISI</option>
- </select>
- <br /><br />
- <input type="submit" name="Submit" value="Submit">
- </form>
- <?php
- if(isset($_POST['Submit'])){
- main();
- }
- function main(){
- $link = connectDB();
- $major=trim($_POST['major']);
- if(verify($major)){
- $prepared = queryData($link, $major);
- showData($prepared);
- }else{
- echo "<p>Please select a major</p>";
- }
- mysqli_close($link);
- }
- function connectDB(){
- dekey_main();
- $host = trim ($GLOBALS['host']);
- $username = trim ($GLOBALS['username']);
- $password = trim ($GLOBALS['password']);
- $db = trim ($GLOBALS['db_name']);
- $myDB = mysqli_connect($host, $username, $password, $db);
- if(mysqli_connect_error()){
- echo "connection error";
- exit;
- }
- return $myDB;
- }
- function verify($major){
- $isValid = false;
- $major=mb_convert_encoding($major);
- $regEx ="^[a-zA-Z]{3,7}$";
- if(size(preg_match($regEx, $major)) > 1){
- $isValid=true;
- }
- return $isValid;
- }
- function queryData($link, $major){
- $major=trim($major);
- if($major=="CIS" || $major=="ISI"){
- $studentQuery="SELECT lastname, firstname, major FROM Students";
- $prepared=mysqli_prepare($link, $studentQuery);
- mysqli_stmt_bind_param($prepared, "s");
- $result=mysqli_stmt_execute($prepared);
- if($result){
- return $prepared;
- }
- }
- return null;
- }
- function showData($prepared){
- if(!is_null($prepared)){
- echo "<table border='0'>";
- echo "<tr>";
- echo "<td> Last Name</td>";
- echo "<td> First Name</td>";
- echo "<td> Major</td>";
- echo "</tr>";
- mysqli_stmt_bind_result ($prepared, $lastName, $firstName, $major);
- while (mysqli_stmt_fetch($prepared)) {
- echo "<tr>";
- echo "<td>$lastName</td>";
- echo "<td>$firstName</td>";
- echo "<td>$major</td>";
- echo "</tr>";
- }
- echo "</table>";
- }else{
- mysqli_stmt_error($prepared);
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement