diff --git a/add_sale.php b/add_sale.php index ca9f559..826c058 100644 --- a/add_sale.php +++ b/add_sale.php @@ -34,15 +34,27 @@ $products = [ ], ]; +require_once __DIR__ . '/db/config.php'; + $success_message = ""; +$error_message = ""; + if ($_SERVER["REQUEST_METHOD"] == "POST") { - // Basic validation if (!empty($_POST['product_name']) && !empty($_POST['quantity']) && is_numeric($_POST['quantity'])) { - // In a real app, you would save this to a database. - // For now, we just show a success message. - $product_name = htmlspecialchars($_POST['product_name']); - $quantity = htmlspecialchars($_POST['quantity']); - $success_message = "Successfully added $quantity sale(s) for $product_name!"; + try { + $product_name = $_POST['product_name']; + $quantity = (int)$_POST['quantity']; + + $pdo = db(); + $stmt = $pdo->prepare("INSERT INTO sales (product_name, quantity) VALUES (?, ?)"); + $stmt->execute([$product_name, $quantity]); + + $success_message = "Successfully added $quantity sale(s) for $product_name!"; + } catch (PDOException $e) { + $error_message = "Error: Could not record the sale. " . $e->getMessage(); + } + } else { + $error_message = "Please fill out all fields correctly."; } } ?> @@ -72,6 +84,12 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { + +