File "add_admin.php"

Full Path: /home/alphpwcp/previewstream.online/admin/add_admin.php
File size: 1.14 KB
MIME-type: text/x-php
Charset: utf-8

<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header('Location: login.php'); exit; }
include '../db.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $hash = password_hash($password, PASSWORD_DEFAULT);
    try {
        $stmt = $pdo->prepare("INSERT INTO admins (username, password) VALUES (?, ?)");
        $stmt->execute([$username, $hash]);
        header('Location: manage_admins.php');
        exit;
    } catch (Exception $e) {
        $error = "Error: username might already exist.";
    }
}
?>

<?php include 'menu.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Add Admin</title><link rel="stylesheet" href="../style.css">
</head>
<body>
<h2>Add New Admin</h2>
<?php if($error): ?><p style="color:red;"><?= htmlspecialchars($error) ?></p><?php endif; ?>
<form method="POST">
    <input type="text" name="username" placeholder="Username" required><br><br>
    <input type="password" name="password" placeholder="Password" required><br><br>
    <button type="submit">Add Admin</button>
</form>
</body></html>