Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <?php
  6. $servername = "localhost:3306";
  7. $username = "root";
  8. $password = "";
  9. $dbname = "university";
  10.  
  11. $conn = null;
  12. try {
  13. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  14. } catch (PDOException $e) {
  15. die('Connection failed: ' . $e->getMessage());
  16. }
  17.  
  18. $groupId = $firstName = $lastName = $facultyNumber = "";
  19.  
  20. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  21. $groupId = modify_input($_POST["groupId"]);
  22. $firstName = modify_input($_POST["firstName"]);
  23. $lastName = modify_input($_POST["lastName"]);
  24. $facultyNumber = modify_input($_POST["facultyNumber"]);
  25.  
  26. $errors = array();
  27.  
  28. if(empty($groupId)) {
  29. array_push($errors,"Group is required");
  30. }
  31. elseif (!ctype_digit($groupId)) {
  32. array_push($errors,"Group has invalid value");
  33. $groupId = "";
  34. }
  35. if(empty($firstName)) {
  36. array_push($errors,"First name is required");
  37. }
  38. if(empty($lastName)) {
  39. array_push($errors,"Last name is required");
  40. }
  41. if(empty($facultyNumber)) {
  42. array_push($errors,"Faculty number is required");
  43. }
  44. elseif (!ctype_digit($facultyNumber)) {
  45. array_push($errors,"Faculty number has invalid value");
  46. $facultyNumber = "";
  47. }
  48.  
  49. if (count($errors) > 0) {
  50. echo '<ul style="color: red;">';
  51. foreach ($errors as $error) {
  52. echo "<li>$error</li>";
  53. }
  54. echo '</ul>';
  55. }
  56. else {
  57. $queryStudents = $conn->prepare("INSERT INTO `students` (`FIRST_NAME`, `LAST_NAME`, `FACULTY_NUMBER`, `GROUP_ID`) VALUES (:firstName, :lastName, :facultyNumber, :groupId)");
  58. $queryStudents->bindParam(':firstName', $firstName);
  59. $queryStudents->bindParam(':lastName', $lastName);
  60. $queryStudents->bindParam(':facultyNumber', $facultyNumber);
  61. $queryStudents->bindParam(':groupId', $groupId);
  62. $queryStudents->execute();
  63. header("Location:university.php");
  64. }
  65. }
  66.  
  67. function modify_input($data) {
  68. $data = trim($data);
  69. $data = stripslashes($data);
  70. $data = htmlspecialchars($data);
  71. return $data;
  72. }
  73. ?>
  74.  
  75. <form action="create.php" method="post">
  76. First name:<br>
  77. <input type="text" name="firstName" value="<?php echo $firstName;?>">
  78. <br>
  79. Last name:<br>
  80. <input type="text" name="lastName" value="<?php echo $lastName;?>">
  81. <br>
  82. Faculty number:<br>
  83. <input type="text" name="facultyNumber" value="<?php echo $facultyNumber;?>">
  84. <br>
  85. Group:<br>
  86. <select name="groupId">
  87. <option value="">Please choose</option>
  88. <?php
  89. $query = "SELECT groups.id, groups.name, majors.name as major_name FROM groups
  90. INNER JOIN majors on groups.major_id = majors.id";
  91. $result = $conn->query($query);
  92. if ($result->rowCount() > 0) {
  93. while($row = $result->fetch(PDO::FETCH_ASSOC)) {
  94. echo '<option value="'.$row["id"].'"';
  95. if($row["id"] == $groupId) {
  96. echo ' selected';
  97. }
  98. echo '>'.$row["major_name"].': '.$row["name"].'</option>';
  99. }
  100. }
  101. ?>
  102. </select>
  103. <br><br>
  104. <input type="submit" value="Submit">
  105. </form>
  106.  
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement