prepare("SELECT COUNT(*) FROM Users WHERE email = ?"); $stmt->execute([$email]); if ($stmt->fetchColumn() > 0) { $errors[] = 'Email address is already registered.'; } else { // Hash password $hashed_password = password_hash($password, PASSWORD_DEFAULT); // Set default role $role = 'Student'; // Insert user into database $sql = "INSERT INTO Users (name, email, password, role, gender, year, department) VALUES (?, ?, ?, ?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); if ($stmt->execute([$name, $email, $hashed_password, $role, $gender, $year, $department])) { $success = 'Registration successful! You can now log in.'; // Send welcome email require_once __DIR__ . '/../mail/MailService.php'; $subject = 'Welcome to Student Hostel'; $body = "

Welcome, {$name}!

Your account has been successfully created. You can now log in and request a room.

"; MailService::sendMail($email, $subject, $body); } else { $errors[] = 'Something went wrong. Please try again later.'; } } } catch (PDOException $e) { $errors[] = "Database error: " . $e->getMessage(); } } } ?>

Sign Up