93 lines
3.7 KiB
PHP
93 lines
3.7 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'auth.php'; // Ensure tables are created
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$username = trim($_POST['username']);
|
|
$password = trim($_POST['password']);
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = "Please enter both username and password.";
|
|
} else {
|
|
$pdo = db();
|
|
|
|
// Check if user already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
if ($stmt->fetch()) {
|
|
$error = "Username already exists. Please choose another one.";
|
|
} else {
|
|
// Get 'teacher' role ID
|
|
$stmt = $pdo->prepare("SELECT id FROM roles WHERE role_name = 'teacher'");
|
|
$stmt->execute();
|
|
$role = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$role) {
|
|
// This is a fallback, 'teacher' role should exist from auth.php
|
|
$error = "Default role 'teacher' not found. Please contact admin.";
|
|
} else {
|
|
$role_id = $role['id'];
|
|
$password_hash = password_hash($password, PASSWORD_BCRYPT);
|
|
|
|
$sql = "INSERT INTO users (username, password_hash, role_id) VALUES (?, ?, ?)";
|
|
$stmt= $pdo->prepare($sql);
|
|
if ($stmt->execute([$username, $password_hash, $role_id])) {
|
|
$_SESSION['success_message'] = "Registration successful! You can now login.";
|
|
header("Location: login.php");
|
|
exit();
|
|
} else {
|
|
$error = "Something went wrong. Please try again later.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Sign Up - Bhuddi School</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Sign Up
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if(!empty($error)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form action="signup.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Sign Up</button>
|
|
</form>
|
|
</div>
|
|
<div class="card-footer text-center">
|
|
Already have an account? <a href="login.php">Login here</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|