37228-vm/db/setup.php
Flatlogic Bot e1bec99a55 1.0
2026-01-01 20:58:27 +00:00

41 lines
1.4 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
$pdo->exec("CREATE TABLE IF NOT EXISTS members (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
joined_at DATETIME NOT NULL,
status VARCHAR(50) NOT NULL DEFAULT 'active'
);");
// Check if table is empty before inserting
$stmt = $pdo->query("SELECT COUNT(*) FROM members");
if ($stmt->fetchColumn() == 0) {
$stmt = $pdo->prepare("INSERT INTO members (name, email, joined_at, status) VALUES (?, ?, ?, ?)");
$members = [
['Alice Johnson', 'alice@example.com', '2023-01-15 10:30:00', 'active'],
['Bob Williams', 'bob@example.com', '2023-02-20 14:00:00', 'active'],
['Charlie Brown', 'charlie@example.com', '2023-03-05 09:00:00', 'active'],
['Diana Miller', 'diana@example.com', '2023-04-10 18:45:00', 'unsubscribed'],
['Ethan Davis', 'ethan@example.com', '2023-05-22 11:20:00', 'active'],
];
foreach ($members as $member) {
$stmt->execute($member);
}
echo "Database setup complete. `members` table created and populated.\n";
} else {
echo "`members` table already exists and contains data. Skipping population.\n";
}
} catch (PDOException $e) {
die("Database setup failed: " . $e->getMessage() . "\n");
}