37434-vm/db/setup.php
Flatlogic Bot 8d321c93b4 v1
2026-01-13 13:13:25 +00:00

50 lines
1.9 KiB
PHP

<?php
require_once 'config.php';
function setup_database() {
$pdo = db();
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
author VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
SQL;
$pdo->exec($sql);
// Check if posts table is empty
$stmt = $pdo->query("SELECT COUNT(*) FROM posts");
$postCount = $stmt->fetchColumn();
if ($postCount == 0) {
$posts = [
[
'title' => 'The Future of Web Development',
'content' => 'The web is constantly evolving. In this post, we explore the latest trends in web development, from serverless architectures to the rise of AI-powered tools. Stay ahead of the curve and learn what to expect in the coming years.',
'author' => 'Jane Doe'
],
[
'title' => 'A Guide to Mindful Coding',
'content' => 'Coding can be stressful. This guide offers practical tips for staying focused, managing complexity, and writing better code through mindfulness. Learn how to bring a sense of calm and clarity to your development process.',
'author' => 'John Smith'
],
[
'title' => 'Mastering CSS Grid',
'content' => 'CSS Grid is a powerful tool for creating complex layouts with ease. This tutorial walks you through the fundamentals of Grid, with practical examples and pro tips to help you master this essential CSS feature.',
'author' => 'Emily White'
]
];
$stmt = $pdo->prepare("INSERT INTO posts (title, content, author) VALUES (?, ?, ?)");
foreach ($posts as $post) {
$stmt->execute([$post['title'], $post['content'], $post['author']]);
}
}
}
setup_database();
?>