Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <?php
- $servername = "localhost:3306";
- $username = "root";
- $password = "";
- $dbname = "university";
- $conn = null;
- try {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- } catch (PDOException $e) {
- die('Connection failed: ' . $e->getMessage());
- }
- $groupId = $firstName = $lastName = $facultyNumber = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $groupId = modify_input($_POST["groupId"]);
- $firstName = modify_input($_POST["firstName"]);
- $lastName = modify_input($_POST["lastName"]);
- $facultyNumber = modify_input($_POST["facultyNumber"]);
- $errors = array();
- if(empty($groupId)) {
- array_push($errors,"Group is required");
- }
- elseif (!ctype_digit($groupId)) {
- array_push($errors,"Group has invalid value");
- $groupId = "";
- }
- if(empty($firstName)) {
- array_push($errors,"First name is required");
- }
- if(empty($lastName)) {
- array_push($errors,"Last name is required");
- }
- if(empty($facultyNumber)) {
- array_push($errors,"Faculty number is required");
- }
- elseif (!ctype_digit($facultyNumber)) {
- array_push($errors,"Faculty number has invalid value");
- $facultyNumber = "";
- }
- if (count($errors) > 0) {
- echo '<ul style="color: red;">';
- foreach ($errors as $error) {
- echo "<li>$error</li>";
- }
- echo '</ul>';
- }
- else {
- $queryStudents = $conn->prepare("INSERT INTO `students` (`FIRST_NAME`, `LAST_NAME`, `FACULTY_NUMBER`, `GROUP_ID`) VALUES (:firstName, :lastName, :facultyNumber, :groupId)");
- $queryStudents->bindParam(':firstName', $firstName);
- $queryStudents->bindParam(':lastName', $lastName);
- $queryStudents->bindParam(':facultyNumber', $facultyNumber);
- $queryStudents->bindParam(':groupId', $groupId);
- $queryStudents->execute();
- header("Location:university.php");
- }
- }
- function modify_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <form action="create.php" method="post">
- First name:<br>
- <input type="text" name="firstName" value="<?php echo $firstName;?>">
- <br>
- Last name:<br>
- <input type="text" name="lastName" value="<?php echo $lastName;?>">
- <br>
- Faculty number:<br>
- <input type="text" name="facultyNumber" value="<?php echo $facultyNumber;?>">
- <br>
- Group:<br>
- <select name="groupId">
- <option value="">Please choose</option>
- <?php
- $query = "SELECT groups.id, groups.name, majors.name as major_name FROM groups
- INNER JOIN majors on groups.major_id = majors.id";
- $result = $conn->query($query);
- if ($result->rowCount() > 0) {
- while($row = $result->fetch(PDO::FETCH_ASSOC)) {
- echo '<option value="'.$row["id"].'"';
- if($row["id"] == $groupId) {
- echo ' selected';
- }
- echo '>'.$row["major_name"].': '.$row["name"].'</option>';
- }
- }
- ?>
- </select>
- <br><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement