36432-vm/register.php
Flatlogic Bot 4ff4f98255 Basic
2025-11-28 15:28:25 +00:00

84 lines
3.4 KiB
PHP

<?php
require_once 'db/config.php';
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$role = $_POST['role'] ?? '';
if (empty($username) || empty($email) || empty($password) || empty($role)) {
$error = 'Please fill in all fields.';
} else {
try {
$db = db();
// Check for existing user
$stmt = $db->prepare('SELECT id FROM users WHERE username = ? OR email = ?');
$stmt->execute([$username, $email]);
if ($stmt->fetch()) {
$error = 'Username or email already exists.';
} else {
// Insert new user
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare('INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)');
$stmt->execute([$username, $email, $hashed_password, $role]);
$success = 'Registration successful! You can now <a href="login.php">login</a>.';
}
} catch (PDOException $e) {
$error = 'Database error: ' . $e->getMessage();
}
}
}
?>
<?php require_once 'header.php'; ?>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h3 class="card-title text-center">Register</h3>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php else: ?>
<form action="register.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="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" 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>
<div class="mb-3">
<label for="role" class="form-label">Role</label>
<select class="form-select" id="role" name="role" required>
<option value="student">Student</option>
<option value="teacher">Teacher</option>
</select>
</div>
<button type="submit" class="btn btn-primary w-100">Register</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php require_once 'footer.php'; ?>