36027-vm/db/seed.php
2025-11-22 14:52:53 +00:00

52 lines
2.1 KiB
PHP

<?php
require_once 'config.php';
function seed_database() {
$pdo = db();
// Check if products table is empty
$stmt = $pdo->query("SELECT COUNT(*) FROM products");
if ($stmt->fetchColumn() > 0) {
echo "Products table is not empty. Skipping seeding.\n";
return;
}
$products = [
[
'name' => 'Gilded Napkin Ring',
'description' => 'A touch of elegance for your dining table. These gilded napkin rings are perfect for special occasions.',
'price' => 12.99,
'image_url' => 'https://images.pexels.com/photos/1034662/pexels-photo-1034662.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'
],
[
'name' => 'Modern Candle Stand',
'description' => 'Sleek and minimalist candle stand, crafted from brushed metal. A perfect centerpiece.',
'price' => 39.99,
'image_url' => 'https://images.pexels.com/photos/3747505/pexels-photo-3747505.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'
],
[
'name' => 'Rustic Wooden Coasters',
'description' => 'Set of four handcrafted wooden coasters. Protect your surfaces with a touch of rustic charm.',
'price' => 19.99,
'image_url' => 'https://images.pexels.com/photos/1309766/pexels-photo-1309766.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'
],
[
'name' => 'Ceramic Bud Vase',
'description' => 'A small, elegant ceramic vase for single stems or small bouquets. Available in various colors.',
'price' => 24.99,
'image_url' => 'https://images.pexels.com/photos/1103970/pexels-photo-1103970.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'
]
];
$stmt = $pdo->prepare("INSERT INTO products (name, description, price, image_url) VALUES (?, ?, ?, ?)");
foreach ($products as $product) {
$stmt->execute([$product['name'], $product['description'], $product['price'], $product['image_url']]);
}
echo "Database seeded successfully.\n";
}
seed_database();