35149-vm/register.php
Flatlogic Bot 7a3409404c v01
2025-10-23 20:44:05 +00:00

129 lines
5.2 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$roles = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name FROM roles ORDER BY name");
$roles = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// If roles table doesn't exist yet, we can proceed without it
// The migration will create it.
}
$errors = [];
$success = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = trim($_POST['email']);
$password = $_POST['password'];
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$role_id = $_POST['role_id'];
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "A valid email is required.";
}
if (empty($password) || strlen($password) < 8) {
$errors[] = "Password must be at least 8 characters long.";
}
if (empty($first_name)) {
$errors[] = "First name is required.";
}
if (empty($last_name)) {
$errors[] = "Last name is required.";
}
if (empty($role_id)) {
$errors[] = "Please select a role.";
}
if (empty($errors)) {
try {
$pdo = db();
// Check if user already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$errors[] = "An account with this email already exists.";
} else {
// Insert new user
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (email, password, first_name, last_name, role_id) VALUES (?, ?, ?, ?, ?)");
if ($stmt->execute([$email, $hashed_password, $first_name, $last_name, $role_id])) {
$success = "Registration successful! You can now <a href='login.php'>log in</a>.";
} else {
$errors[] = "Something went wrong. Please try again later.";
}
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
include 'templates/header.php';
?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h2>Register</h2>
</div>
<div class="card-body">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success">
<p><?php echo $success; ?></p>
</div>
<?php else: ?>
<form action="register.php" method="post">
<div class="form-group mb-3">
<label for="first_name">First Name</label>
<input type="text" class="form-control" id="first_name" name="first_name" required>
</div>
<div class="form-group mb-3">
<label for="last_name">Last Name</label>
<input type="text" class="form-control" id="last_name" name="last_name" required>
</div>
<div class="form-group mb-3">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="form-group mb-3">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" minlength="8" required>
</div>
<div class="form-group mb-3">
<label for="role_id">Role</label>
<select class="form-control" id="role_id" name="role_id" required>
<option value="">-- Select a Role --</option>
<?php foreach ($roles as $role): ?>
<option value="<?php echo htmlspecialchars($role['id']); ?>">
<?php echo htmlspecialchars($role['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
<?php endif; ?>
</div>
<div class="card-footer text-center">
<p>Already have an account? <a href="login.php">Login here</a>.</p>
</div>
</div>
</div>
</div>
</div>
<?php include 'templates/footer.php'; ?>