36952-vm/add_user.php
Flatlogic Bot 76d7d99142 PMS 1
2025-12-15 01:31:18 +00:00

60 lines
1.9 KiB
PHP

<?php
require_once 'session.php';
check_admin();
require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
$role = $_POST['role'];
if ($username && $password && $role) {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
try {
$db = db();
$stmt = $db->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
$stmt->execute([$username, $hashed_password, $role]);
header("Location: users.php?success=User added");
exit;
} catch (PDOException $e) {
$error = "Error: " . $e->getMessage();
}
} else {
$error = "Please fill all required fields.";
}
}
include 'templates/header.php';
?>
<div class="container mt-4">
<h2>Add New User</h2>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form action="add_user.php" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="form-group">
<label for="role">Role</label>
<select class="form-control" id="role" name="role">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Add User</button>
<a href="users.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
<?php include 'templates/footer.php'; ?>