|  | 
|  | 1 | +<?php | 
|  | 2 | +$dsn = 'mysql:host=your_host;dbname=your_database'; | 
|  | 3 | +$username = 'your_username'; | 
|  | 4 | +$password = 'your_password'; | 
|  | 5 | + | 
|  | 6 | +try { | 
|  | 7 | + $pdo = new PDO($dsn, $username, $password); | 
|  | 8 | + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | 
|  | 9 | +} catch (PDOException $e) { | 
|  | 10 | + die("Database connection failed: " . $e->getMessage()); | 
|  | 11 | +} | 
|  | 12 | + | 
|  | 13 | +if ($_SERVER['REQUEST_METHOD'] === 'POST') { | 
|  | 14 | + $hashedPassword = password_hash($_POST['password'], PASSWORD_DEFAULT); | 
|  | 15 | + | 
|  | 16 | + $stmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (:username, :password, 'admin')"); | 
|  | 17 | + $stmt->execute([ | 
|  | 18 | + ':username' => $_POST['username'], | 
|  | 19 | + ':password' => $hashedPassword | 
|  | 20 | + ]); | 
|  | 21 | + echo "Admin user created successfully."; | 
|  | 22 | +} | 
|  | 23 | +?> | 
|  | 24 | + | 
|  | 25 | +<!DOCTYPE html> | 
|  | 26 | +<html lang="en"> | 
|  | 27 | +<head> | 
|  | 28 | + <meta charset="UTF-8"> | 
|  | 29 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> | 
|  | 30 | + <title>Register Admin</title> | 
|  | 31 | +</head> | 
|  | 32 | +<body> | 
|  | 33 | + <h1>Register Admin</h1> | 
|  | 34 | + <form method="post"> | 
|  | 35 | + <label for="username">Username:</label> | 
|  | 36 | + <input type="text" name="username" id="username" required> | 
|  | 37 | + <br> | 
|  | 38 | + <label for="password">Password:</label> | 
|  | 39 | + <input type="password" name="password" id="password" required> | 
|  | 40 | + <br> | 
|  | 41 | + <button type="submit">Register</button> | 
|  | 42 | + </form> | 
|  | 43 | +</body> | 
|  | 44 | +</html> | 
0 commit comments