function studentSignin($pdo) { $student_info = new StudentInfo(); //$student_id = $student_info->generateId(); $fname = $student_info->firstName(); $lname = $student_info->lastName(); session_unset(); $query = "SELECT * FROM student WHERE student_fname = :fname AND student_lname = :lname"; $stmt = $pdo->prepare($query); $stmt->bindParam(':fname', $fname); $stmt->bindParam(':lname', $lname); $stmt->execute(); if ($student_exists = $stmt->fetch()) { foreach ($student_exists as $student) { $_SESSION['user_id'] = $student['student_id']; $_SESSION['f_name'] = $student['student_fname']; $_SESSION['l_name'] = $student['student_lname']; $_SESSION['theme'] = "red"; $_SESSION['rank'] = 1; } } else { $student_id = $student_info->generateId(); $_SESSION['user_id'] = $student_id; $_SESSION['f_name'] = $fname; $_SESSION['l_name'] = $lname; $_SESSION['theme'] = "red"; $_SESSION['rank'] = 1; $query = "INSERT INTO student (student_id, student_fname, student_lname) VALUES (:student_id, :fname, :lname)"; $stmt = $pdo->prepare($query); $stmt->bindParam(':student_id', $student_id, PDO::PARAM_INT); $stmt->bindParam(':fname', $fname); $stmt->bindParam(':lname', $lname); $stmt->execute(); //$course_id = rand(1,3); $query = "SELECT * FROM class"; // This part can just be replaced with a COUNT function $stmt = $pdo->prepare($query); $stmt->execute(); $num_classes = $stmt->rowCount(); $classes_enrolled = array(); $classes_enrolled[0] = rand(1, 3); while (($i = rand(1, 3)) == $classes_enrolled[0]) { } $classes_enrolled[1] = $i; while (($i = rand(1, 3)) == $classes_enrolled[0] || $i == $classes_enrolled[1]) { } $classes_enrolled[2] = $i; for ($i = 0; $i < 3; $i++) { $query = "INSERT INTO enroll (enroll_student_id, enroll_class_id) VALUES (:student_id, :course_id)"; $stmt = $pdo->prepare($query); $stmt->bindParam(':student_id', $student_id); $stmt->bindParam(':course_id', $classes_enrolled[$i]); $stmt->execute(); } } $pdo = null; }
function studentSignin() { try { $pdo = new PDO(DB_PDODRIVER . ':host=' . DB_HOST . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS); } catch (\PDOException $e) { echo "Connection failed: " . $e->getMessage(); exit; } $student_info = new StudentInfo(); //$student_id = $student_info->generateId(); $fname = $student_info->firstName(); $lname = $student_info->lastName(); session_unset(); $query = "SELECT * FROM student WHERE student_fname = :fname AND student_lname = :lname"; $stmt = $pdo->prepare($query); $stmt->bindParam(':fname', $fname); $stmt->bindParam(':lname', $lname); $stmt->execute(); if ($stmt->fetch(PDO::FETCH_ASSOC)) { while ($student = $stmt->fetch(PDO::FETCH_ASSOC)) { $_SESSION['user_id'] = $student['student_id']; $_SESSION['f_name'] = $student['student_fname']; $_SESSION['l_name'] = $student['student_lname']; $_SESSION['theme'] = "red"; $_SESSION['rank'] = 1; } } else { $student_id = $student_info->generateId(); $_SESSION['user_id'] = $student_id; $_SESSION['f_name'] = $fname; $_SESSION['l_name'] = $lname; $_SESSION['theme'] = "red"; $_SERVER['rank'] = 1; $query = "INSERT INTO student (student_id, student_fname, student_lname) VALUES (:student_id, :fname, :lname)"; $stmt = $pdo->prepare($query); $stmt->bindParam(':student_id', $student_id, PDO::PARAM_INT); $stmt->bindParam(':fname', $fname); $stmt->bindParam(':lname', $lname); $stmt->execute(); //$course_id = rand(1,3); $query = "SELECT * FROM class"; // This part can just be replaced with a COUNT function $stmt = $pdo->prepare($query); $stmt->execute(); $num_classes = $stmt->rowCount(); $classes_enrolled = array(); $classes_enrolled[0] = rand(1, 3); while (($i = rand(1, 3)) == $classes_enrolled[0]) { } $classes_enrolled[1] = $i; while (($i = rand(1, 3)) == $classes_enrolled[0] || $i == $classes_enrolled[1]) { } $classes_enrolled[2] = $i; for ($i = 0; $i < 3; $i++) { $query = "INSERT INTO enroll (enroll_student_id, enroll_class_id) VALUES (:student_id, :course_id)"; $stmt = $pdo->prepare($query); $stmt->bindParam(':student_id', $student_id); $stmt->bindParam(':course_id', $classes_enrolled[$i]); $stmt->execute(); } } $pdo = null; }