88 lines
4.0 KiB
PHP
88 lines
4.0 KiB
PHP
<?php
|
|
// db/setup.php
|
|
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
/**
|
|
* Creates the products table and inserts sample data.
|
|
* This function is idempotent and safe to run multiple times.
|
|
*/
|
|
function setup_database() {
|
|
try {
|
|
$pdo = db_connect();
|
|
if (!$pdo) {
|
|
throw new RuntimeException('Database connection failed.');
|
|
}
|
|
|
|
// Create the products table if it doesn't exist
|
|
$pdo->exec("\n CREATE TABLE IF NOT EXISTS products (\n id INT AUTO_INCREMENT PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n price DECIMAL(10, 2) NOT NULL,\n image_url VARCHAR(255),\n stock INT DEFAULT 0,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n );\n ");
|
|
|
|
// Check if the table is empty before inserting data
|
|
$stmt = $pdo->query('SELECT COUNT(*) FROM products');
|
|
if ($stmt->fetchColumn() == 0) {
|
|
// Insert sample products
|
|
$products = [
|
|
[
|
|
'name' => 'QuantumBook Pro',
|
|
'description' => 'The next generation of laptops with quantum processing power. Sleek, powerful, and unbelievably fast.',
|
|
'price' => 2499.99,
|
|
'image_url' => 'https://picsum.photos/seed/laptop/600/600',
|
|
'stock' => 50
|
|
],
|
|
[
|
|
'name' => 'NovaPhone X',
|
|
'description' => 'A revolutionary smartphone with a holographic display and an AI-powered camera system.',
|
|
'price' => 1299.99,
|
|
'image_url' => 'https://picsum.photos/seed/phone/600/600',
|
|
'stock' => 120
|
|
],
|
|
[
|
|
'name' => 'AuraBuds Pro',
|
|
'description' => 'Immersive sound experience with active noise-cancellation and a 48-hour battery life.',
|
|
'price' => 249.99,
|
|
'image_url' => 'https://picsum.photos/seed/headphones/600/600',
|
|
'stock' => 300
|
|
],
|
|
[
|
|
'name' => 'GalaxyPad 5',
|
|
'description' => 'A versatile tablet for work and play, featuring a stunning 12.9-inch display and stylus support.',
|
|
'price' => 799.99,
|
|
'image_url' => 'https://picsum.photos/seed/tablet/600/600',
|
|
'stock' => 80
|
|
],
|
|
[
|
|
'name' => 'ChronoWatch 2',
|
|
'description' => 'A smart watch that tracks your fitness, sleep, and notifications with a classic, elegant design.',
|
|
'price' => 449.99,
|
|
'image_url' => 'https://picsum.photos/seed/watch/600/600',
|
|
'stock' => 150
|
|
],
|
|
[
|
|
'name' => 'VR-Xperience Helmet',
|
|
'description' => 'Step into new worlds with this high-fidelity virtual reality helmet. True immersion awaits.',
|
|
'price' => 999.99,
|
|
'image_url' => 'https://picsum.photos/seed/vr/600/600',
|
|
'stock' => 40
|
|
]
|
|
];
|
|
|
|
$stmt = $pdo->prepare('INSERT INTO products (name, description, price, image_url, stock) VALUES (?, ?, ?, ?, ?)');
|
|
foreach ($products as $product) {
|
|
$stmt->execute([$product['name'], $product['description'], $product['price'], $product['image_url'], $product['stock']]);
|
|
}
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
error_log('Database Setup Error: ' . $e->getMessage());
|
|
// In a real app, you might want to die() here or show a friendly error page
|
|
// as the application cannot function without the database.
|
|
}
|
|
}
|
|
|
|
// If the script is run directly from the command line, execute the setup.
|
|
if (php_sapi_name() === 'cli') {
|
|
echo "Setting up database...\n";
|
|
setup_database();
|
|
echo "Database setup complete.\n";
|
|
}
|