prepare("SELECT * FROM products WHERE id = ?"); $stmt->execute([$id]); $product = $stmt->fetch(); if (!$product) { admin_set_flash('error', 'Product not found'); header('Location: ' . ADMIN_PATH . 'pages/products.php'); exit; } $categories = get_categories(); $brands = $db->query("SELECT * FROM brands WHERE status = 1")->fetchAll(); $product_images = get_product_images($id); if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (verify_csrf($_POST[CSRF_TOKEN_NAME] ?? '')) { $name = trim($_POST['name']); $slug = $product['slug']; if ($name !== $product['name']) { $slug = create_slug($name) . '-' . $id; } $category_id = intval($_POST['category_id']); $subcategory_id = !empty($_POST['subcategory_id']) ? intval($_POST['subcategory_id']) : null; $brand_id = !empty($_POST['brand_id']) ? intval($_POST['brand_id']) : null; $short_description = trim($_POST['short_description'] ?? ''); $description = trim($_POST['description'] ?? ''); $specifications = trim($_POST['specifications'] ?? ''); $regular_price = floatval($_POST['regular_price']); $sale_price = !empty($_POST['sale_price']) ? floatval($_POST['sale_price']) : null; $sku = trim($_POST['sku'] ?? ''); $sku = $sku !== '' ? $sku : null; $stock_quantity = intval($_POST['stock_quantity'] ?? 0); $stock_status = $_POST['stock_status'] ?? 'in_stock'; $tags = trim($_POST['tags'] ?? ''); $meta_title = trim($_POST['meta_title'] ?? ''); $meta_description = trim($_POST['meta_description'] ?? ''); $featured = isset($_POST['featured']) ? 1 : 0; $best_seller = isset($_POST['best_seller']) ? 1 : 0; $new_arrival = isset($_POST['new_arrival']) ? 1 : 0; $discount = 0; if ($sale_price && $regular_price > 0) { $discount = (int)round((($regular_price - $sale_price) / $regular_price) * 100); } $thumbnail = $product['thumbnail']; if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] == 0) { $new_thumb = upload_image($_FILES['thumbnail'], 'products', $name); if ($new_thumb) { if ($thumbnail && file_exists(UPLOADS_PATH . 'products/' . $thumbnail)) { @unlink(UPLOADS_PATH . 'products/' . $thumbnail); } $thumbnail = $new_thumb; } else { $error = 'Image upload failed. Please check that the file is JPG/PNG/GIF/WebP and under 5MB, and that assets/uploads/products/ folder is writable.'; } } elseif (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] != 0 && $_FILES['thumbnail']['error'] != 4) { $error = 'Image upload error (code: ' . $_FILES['thumbnail']['error'] . '). Please try again.'; } if (!isset($error)) { try { $db->beginTransaction(); $stmt = $db->prepare("UPDATE products SET category_id=?, subcategory_id=?, brand_id=?, name=?, slug=?, short_description=?, description=?, specifications=?, regular_price=?, sale_price=?, discount_percent=?, sku=?, stock_quantity=?, stock_status=?, featured=?, best_seller=?, new_arrival=?, thumbnail=?, tags=?, meta_title=?, meta_description=? WHERE id=?"); $stmt->execute([$category_id, $subcategory_id, $brand_id, $name, $slug, $short_description, $description, $specifications, $regular_price, $sale_price, $discount, $sku, $stock_quantity, $stock_status, $featured, $best_seller, $new_arrival, $thumbnail, $tags, $meta_title, $meta_description, $id]); // Handle additional images if (isset($_FILES['images']) && !empty($_FILES['images']['name'][0])) { $count = count($_FILES['images']['name']); $max_sort_stmt = $db->prepare("SELECT MAX(sort_order) FROM product_images WHERE product_id = ?"); $max_sort_stmt->execute([$id]); $sort = (int)$max_sort_stmt->fetchColumn() + 1; for ($i = 0; $i < $count; $i++) { if ($_FILES['images']['error'][$i] == 0) { $file = [ 'name' => $_FILES['images']['name'][$i], 'type' => $_FILES['images']['type'][$i], 'tmp_name' => $_FILES['images']['tmp_name'][$i], 'error' => $_FILES['images']['error'][$i], 'size' => $_FILES['images']['size'][$i] ]; $image = upload_image($file, 'products', $name . '-' . $id . '-' . $i); if ($image) { $db->prepare("INSERT INTO product_images (product_id, image, sort_order) VALUES (?, ?, ?)") ->execute([$id, $image, $sort++]); } } } } $db->commit(); admin_set_flash('success', 'Product updated successfully'); header('Location: ' . ADMIN_PATH . 'pages/products.php'); exit; } catch (Exception $e) { if ($db->inTransaction()) { $db->rollBack(); } $error = 'Update failed: ' . $e->getMessage(); } } } else { $error = 'Security token expired. Please refresh the page and try again.'; } } // Delete additional image if (isset($_GET['delete_image'])) { $img_id = intval($_GET['delete_image']); $img_stmt = $db->prepare("SELECT image FROM product_images WHERE id = ? AND product_id = ?"); $img_stmt->execute([$img_id, $id]); $img = $img_stmt->fetch(); if ($img) { @unlink(UPLOADS_PATH . 'products/' . $img['image']); $db->prepare("DELETE FROM product_images WHERE id = ?")->execute([$img_id]); } header('Location: ' . ADMIN_PATH . 'pages/product-edit.php?id=' . $id); exit; } ?>
Basic Information
Pricing & Inventory
Visibility
>
>
>

Product Images

SEO & Tags

Cancel