Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
- $res = $db->prepare("INSERT INTO students (first_name, last_name, student_id, phone) VALUES (:first_name, :last_name, :student_id, :phone)");
- $n = intval(trim(fgets(STDIN)));
- for ($i = 0; $i < $n; $i++) {
- try {
- $input = explode(" ", trim(fgets(STDIN)));
- $first_name = $input[0];
- $last_name = $input[1];
- $student_id = $input[2];
- $phone = null;
- if ($first_name == '') {
- throw new PDOException("First Name cannot be empty!");
- }
- elseif (preg_match('~[0-9]~', $first_name)) {
- throw new PDOException("First Name must contain only letters!");
- }
- elseif ($last_name == '') {
- throw new PDOException("Last Name cannot be empty!");
- }
- elseif (preg_match('~[0-9]~', $last_name)) {
- throw new PDOException("Last Name must contain only letters!");
- }
- elseif (!is_numeric($student_id)) {
- throw new PDOException("Invalid Student ID");
- }
- $res->execute([':first_name' => $first_name, 'last_name' => $last_name, 'student_id' => $student_id, 'phone' => $phone]);
- $res = null;
- $db = null;
- }
- catch (PDOException $e) {
- echo $e->getMessage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement