-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_setup.php
More file actions
44 lines (38 loc) 路 1.46 KB
/
Copy pathdb_setup.php
File metadata and controls
44 lines (38 loc) 路 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
$host = "localhost";
$dbname = "login_system";
$username = "root";
$password = "";
try {
$pdo = new PDO("mysql:host=$host", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname`");
$pdo->exec("USE `$dbname`");
$pdo->exec("CREATE TABLE IF NOT EXISTS `users` (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`full_name` VARCHAR(100) NOT NULL,
`username` VARCHAR(50) NOT NULL UNIQUE,
`email` VARCHAR(100) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = :username");
$stmt->execute(['username' => 'demo']);
if ($stmt->fetchColumn() == 0) {
$demoPassword = password_hash('demo123', PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (full_name, username, email, password) VALUES (:full_name, :username, :email, :password)");
$stmt->execute([
'full_name' => 'Demo User',
'username' => 'demo',
'email' => 'demo@example.com',
'password' => $demoPassword
]);
echo "Demo user created successfully!<br>";
} else {
echo "Demo user already exists!<br>";
}
echo "Database setup complete!";
} catch(PDOException $e) {
die("Database Error: " . $e->getMessage());
}
?>