prepare("SELECT * FROM products WHERE product_id = ? AND owner_id = ?"); $stmt->execute([$product_id, $_SESSION['user_id']]); $product = $stmt->fetch(); if (!$product) { header('Location: dashboard.php'); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name']); $description = trim($_POST['description']); $price = trim($_POST['price']); $category = trim($_POST['category']); $subcategory = trim($_POST['subcategory']); $stock_quantity = (int)$_POST['stock_quantity']; // Get size data if category is clothes $size_data = null; if ($category === 'clothes' && isset($_POST['sizes']) && isset($_POST['size_quantities'])) { $sizes = $_POST['sizes']; $quantities = $_POST['size_quantities']; $size_data = []; for ($i = 0; $i < count($sizes); $i++) { if (!empty($sizes[$i]) && isset($quantities[$i]) && $quantities[$i] > 0) { $size_data[$sizes[$i]] = (int)$quantities[$i]; } } // Update stock quantity based on sizes $stock_quantity = array_sum($size_data); } // Validate inputs if (empty($name) || empty($description) || empty($price) || empty($stock_quantity)) { $error_message = 'Please fill in all required fields.'; } elseif (!is_numeric($price) || $price <= 0) { $error_message = 'Please enter a valid price.'; } elseif ($stock_quantity < 0) { $error_message = 'Stock quantity cannot be negative.'; } elseif ($category === 'clothes' && empty($size_data)) { $error_message = 'Please add at least one size with quantity.'; } else { try { // Handle image upload $image_path = $product['image_path']; // Keep existing image by default if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $upload_dir = '../uploads/products/'; if (!file_exists($upload_dir)) { mkdir($upload_dir, 0777, true); } $file_extension = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION)); $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif']; if (!in_array($file_extension, $allowed_extensions)) { throw new Exception('Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.'); } $image_path = uniqid() . '.' . $file_extension; $upload_path = $upload_dir . $image_path; if (!move_uploaded_file($_FILES['image']['tmp_name'], $upload_path)) { throw new Exception('Failed to upload image.'); } // Delete old image if exists if ($product['image_path'] && file_exists($upload_dir . $product['image_path'])) { unlink($upload_dir . $product['image_path']); } } if (empty($error_message)) { $pdo->beginTransaction(); $stmt = $pdo->prepare(" UPDATE products SET name = ?, description = ?, price = ?, category = ?, Sub_Cat = ?, stock_quantity = ?, image_path = ?, size_data = ? WHERE product_id = ? AND owner_id = ? "); $stmt->execute([ $name, $description, $price, $category, $subcategory, $stock_quantity, $image_path, $size_data ? json_encode($size_data) : null, $product_id, $_SESSION['user_id'] ]); $pdo->commit(); $success_message = 'Product updated successfully!'; // Refresh product data $stmt = $pdo->prepare("SELECT * FROM products WHERE product_id = ? AND owner_id = ?"); $stmt->execute([$product_id, $_SESSION['user_id']]); $product = $stmt->fetch(); } } catch (Exception $e) { $pdo->rollBack(); $error_message = $e->getMessage(); } } } ?>