Dark-shadow

Student Data page code

Oct 12th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Function to query information based on
  5. * a parameter: in this case, location.
  6. *
  7. */
  8.  
  9. if (isset($_POST['submit'])) {
  10. try {
  11.  
  12. $host = "localhost";
  13. $username = "root";
  14. $password = "";
  15. $dbname = "test";
  16. $dsn = "mysql:host=$host;dbname=$dbname";
  17. $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
  18. function escape($html) { return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8"); }
  19.  
  20. $connection = new PDO($dsn, $username, $password, $options);
  21.  
  22. $sql = "SELECT *
  23. FROM student
  24. WHERE id = :id";
  25.  
  26. $id = $_POST['id'];
  27.  
  28. $statement = $connection->prepare($sql);
  29. $statement->bindParam(':id', $id, PDO::PARAM_STR);
  30. $statement->execute();
  31.  
  32. $result = $statement->fetchAll();
  33. } catch(PDOException $error) {
  34. echo $sql . "<br>" . $error->getMessage();
  35. }
  36. }
  37. ?>
  38.  
  39. <center><h1>MBA P1 Student's Database App</h1>
  40. <style>
  41. label { display: block; margin: 5px 0; }
  42. table { border-collapse: collapse; border-spacing: 0; } td,
  43. th { padding: 5px; border-bottom: 1px solid #aaa; }
  44. </style>
  45. <?php
  46. if (isset($_POST['submit'])) {
  47. if ($result && $statement->rowCount() > 0) { ?>
  48. <h2>Results</h2>
  49.  
  50. <table>
  51. <thead>
  52. <tr>
  53. <th>ID</th>
  54. <th>Full Name</th>
  55.  
  56. <th>Student ID</th>
  57.  
  58.  
  59. </tr>
  60. </thead>
  61. <tbody>
  62. <?php foreach ($result as $row) { ?>
  63. <tr>
  64. <td><?php echo escape($row["id"]); ?></td>
  65. <td><?php echo escape($row["fullname"]); ?></td>
  66. <td><?php echo escape($row["student_id"]); ?></td>
  67. </tr>
  68. <?php } ?>
  69. </tbody>
  70. </table>
  71. <?php } else { ?>
  72. <blockquote>No results found for <?php echo escape($_POST['id']); ?>.</blockquote>
  73. <?php }
  74. } ?>
  75.  
  76. <h2>Find Student's Full Name</h2>
  77.  
  78. <form method="post">
  79. <label for="id">Last 2 Digit of id's</label>
  80. <input type="text" id="id" name="id">
  81. <input type="submit" name="submit" value="View">
  82. </form>
Advertisement
Add Comment
Please, Sign In to add comment