33 lines
1.1 KiB
PHP
33 lines
1.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = $_POST['name'] ?? '';
|
|
$sku = $_POST['sku'] ?? null;
|
|
$category = $_POST['category'] ?? null;
|
|
$price = $_POST['price'] ?? 0;
|
|
$stock = $_POST['stock'] ?? 0;
|
|
|
|
// Basic validation
|
|
if (empty($name) || !is_numeric($price) || !is_numeric($stock)) {
|
|
// Handle error - maybe redirect back with an error message
|
|
header('Location: product_add.php?error=Invalid+input');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO products (name, sku, category, price, stock) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$name, $sku, $category, $price, $stock]);
|
|
|
|
// Redirect to product list on success
|
|
header('Location: products.php?success=Product+added');
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
// Handle error, e.g., duplicate SKU
|
|
// For now, we'll just redirect with a generic error
|
|
error_log($e->getMessage());
|
|
header('Location: product_add.php?error=Could+not+add+product');
|
|
exit;
|
|
}
|
|
}
|