prepare("SELECT * FROM orders WHERE id = ?"); $stmt->execute([$id]); $order = $stmt->fetch(); if (!$order) { header("Location: orders.php"); exit; } } catch (PDOException $e) { die("Database lookup error: " . $e->getMessage()); } $error = ''; $success = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $customer_name = trim($_POST['customer_name'] ?? ''); $email = trim($_POST['email'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $whatsapp = trim($_POST['whatsapp'] ?? ''); $address = trim($_POST['address'] ?? ''); $curtain_type = trim($_POST['curtain_type'] ?? ''); $fabric = trim($_POST['fabric'] ?? ''); $color = trim($_POST['color'] ?? ''); $width = filter_var($_POST['width'] ?? null, FILTER_VALIDATE_FLOAT); $height = filter_var($_POST['height'] ?? null, FILTER_VALIDATE_FLOAT); $quantity = filter_var($_POST['quantity'] ?? 1, FILTER_VALIDATE_INT); $notes = trim($_POST['notes'] ?? ''); $status = trim($_POST['status'] ?? ''); // Form validations if (empty($customer_name) || empty($email) || empty($phone) || empty($whatsapp) || empty($address) || empty($curtain_type) || empty($fabric) || empty($color) || $width === false || $height === false || $quantity === false || empty($status)) { $error = 'Please fill in all required fields with valid values.'; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error = 'Please enter a valid email address.'; } elseif ($width <= 0 || $height <= 0 || $quantity <= 0) { $error = 'Width, Height, and Quantity must be positive values.'; } else { try { $sql = "UPDATE orders SET customer_name = :customer_name, email = :email, phone = :phone, whatsapp = :whatsapp, address = :address, curtain_type = :curtain_type, fabric = :fabric, color = :color, width = :width, height = :height, quantity = :quantity, notes = :notes, status = :status WHERE id = :id"; $update_stmt = $pdo->prepare($sql); $update_stmt->execute([ ':customer_name' => $customer_name, ':email' => $email, ':phone' => $phone, ':whatsapp' => $whatsapp, ':address' => $address, ':curtain_type' => $curtain_type, ':fabric' => $fabric, ':color' => $color, ':width' => $width, ':height' => $height, ':quantity' => $quantity, ':notes' => $notes, ':status' => $status, ':id' => $id ]); $success = 'Order updated successfully!'; // Reload order specs $stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ?"); $stmt->execute([$id]); $order = $stmt->fetch(); } catch (PDOException $e) { $error = 'Failed to update order: ' . $e->getMessage(); } } } include '../includes/admin_header.php'; ?>