🤩 File Manager - Mr.X
PHP:
8.3.33
Server:
Apache
OS:
Linux 5.14.0-611.49.1.el9_7.x86_64
User:
websparkit
Navigate
Upload:
Upload
New File
New Folder
Editing:product-edit.php
<?php $page = 'products'; $page_title = 'Edit Product'; require_once '../includes/header.php'; $id = intval($_GET['id'] ?? 0); $stmt = $db->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; } ?> <div class="admin-card"> <?php if (isset($error)): ?> <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div> <?php endif; ?> <form method="POST" enctype="multipart/form-data"> <?= csrf_field() ?> <div class="row g-4"> <div class="col-lg-8"> <h5 style="margin-bottom: 20px; color: var(--primary-rose-gold);">Basic Information</h5> <div class="form-group-admin"> <label class="form-label-admin">Product Name *</label> <input type="text" name="name" class="form-control-admin" required value="<?= htmlspecialchars($product['name']) ?>"> </div> <div class="row g-3"> <div class="col-md-6"> <div class="form-group-admin"> <label class="form-label-admin">Category *</label> <select name="category_id" class="form-control-admin" required> <?php foreach ($categories as $cat): ?> <option value="<?= $cat['id'] ?>" <?= $product['category_id'] == $cat['id'] ? 'selected' : '' ?>> <?= htmlspecialchars($cat['name']) ?> </option> <?php endforeach; ?> </select> </div> </div> <div class="col-md-6"> <div class="form-group-admin"> <label class="form-label-admin">Brand</label> <select name="brand_id" class="form-control-admin"> <option value="">Select Brand</option> <?php foreach ($brands as $brand): ?> <option value="<?= $brand['id'] ?>" <?= $product['brand_id'] == $brand['id'] ? 'selected' : '' ?>> <?= htmlspecialchars($brand['name']) ?> </option> <?php endforeach; ?> </select> </div> </div> </div> <div class="form-group-admin"> <label class="form-label-admin">Short Description</label> <textarea name="short_description" class="form-control-admin" rows="2"><?= htmlspecialchars($product['short_description'] ?? '') ?></textarea> </div> <div class="form-group-admin"> <label class="form-label-admin">Full Description</label> <textarea name="description" class="form-control-admin" rows="6"><?= htmlspecialchars($product['description'] ?? '') ?></textarea> </div> <div class="form-group-admin"> <label class="form-label-admin">Specifications (JSON format)</label> <textarea name="specifications" class="form-control-admin" rows="3"><?= htmlspecialchars($product['specifications'] ?? '') ?></textarea> </div> </div> <div class="col-lg-4"> <h5 style="margin-bottom: 20px; color: var(--primary-rose-gold);">Pricing & Inventory</h5> <div class="form-group-admin"> <label class="form-label-admin">Regular Price (₹) *</label> <input type="number" name="regular_price" class="form-control-admin" step="0.01" min="0" required value="<?= $product['regular_price'] ?>"> </div> <div class="form-group-admin"> <label class="form-label-admin">Sale Price (₹)</label> <input type="number" name="sale_price" class="form-control-admin" step="0.01" min="0" value="<?= $product['sale_price'] ?>"> </div> <div class="form-group-admin"> <label class="form-label-admin">SKU</label> <input type="text" name="sku" class="form-control-admin" value="<?= htmlspecialchars($product['sku'] ?? '') ?>"> </div> <div class="form-group-admin"> <label class="form-label-admin">Stock Quantity *</label> <input type="number" name="stock_quantity" class="form-control-admin" min="0" required value="<?= $product['stock_quantity'] ?>"> </div> <div class="form-group-admin"> <label class="form-label-admin">Stock Status</label> <select name="stock_status" class="form-control-admin"> <option value="in_stock" <?= $product['stock_status'] == 'in_stock' ? 'selected' : '' ?>>In Stock</option> <option value="out_of_stock" <?= $product['stock_status'] == 'out_of_stock' ? 'selected' : '' ?>>Out of Stock</option> <option value="pre_order" <?= $product['stock_status'] == 'pre_order' ? 'selected' : '' ?>>Pre-Order</option> </select> </div> <h5 style="margin: 20px 0 15px; color: var(--primary-rose-gold);">Visibility</h5> <div class="form-group-admin"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="featured" name="featured" <?= $product['featured'] ? 'checked' : '' ?>> <label class="form-check-label" for="featured">Featured Product</label> </div> <div class="form-check"> <input class="form-check-input" type="checkbox" id="best_seller" name="best_seller" <?= $product['best_seller'] ? 'checked' : '' ?>> <label class="form-check-label" for="best_seller">Best Seller</label> </div> <div class="form-check"> <input class="form-check-input" type="checkbox" id="new_arrival" name="new_arrival" <?= $product['new_arrival'] ? 'checked' : '' ?>> <label class="form-check-label" for="new_arrival">New Arrival</label> </div> </div> </div> </div> <hr style="margin: 30px 0;"> <h5 style="margin-bottom: 20px; color: var(--primary-rose-gold);">Product Images</h5> <div class="row g-3 mb-3"> <?php foreach ($product_images as $img): ?> <div class="col-md-2"> <div style="position: relative;"> <?php $img_exists = file_exists(UPLOADS_PATH . 'products/' . $img['image']); ?> <img src="<?= $img_exists ? UPLOADS_URL . 'products/' . $img['image'] : ASSETS_PATH . 'images/no-image.jpg' ?>" class="image-preview image-preview-lg w-100" style="object-fit: cover;" onerror="this.onerror=null;this.src='<?= ASSETS_PATH ?>images/no-image.jpg';"> <a href="?id=<?= $id ?>&delete_image=<?= $img['id'] ?>" class="btn-icon btn-admin btn-danger-admin confirm-delete" style="position: absolute; top: 5px; right: 5px;" data-bs-toggle="tooltip" title="Delete Image"> <i class="fas fa-times"></i> </a> </div> </div> <?php endforeach; ?> </div> <div class="row g-3"> <div class="col-md-6"> <div class="form-group-admin"> <label class="form-label-admin">Main Thumbnail</label> <input type="file" name="thumbnail" class="form-control-admin" accept="image/*" data-preview="thumb-preview"> <?php if ($product['thumbnail']): ?> <img src="<?= file_exists(UPLOADS_PATH . 'products/' . $product['thumbnail']) ? UPLOADS_URL . 'products/' . $product['thumbnail'] : ASSETS_PATH . 'images/no-image.jpg' ?>" class="image-preview image-preview-lg" style="margin-top: 10px; object-fit: cover;" onerror="this.onerror=null;this.src='<?= ASSETS_PATH ?>images/no-image.jpg';"> <?php endif; ?> </div> </div> <div class="col-md-6"> <div class="form-group-admin"> <label class="form-label-admin">Add More Images</label> <input type="file" name="images[]" class="form-control-admin" accept="image/*" multiple> </div> </div> </div> <hr style="margin: 30px 0;"> <h5 style="margin-bottom: 20px; color: var(--primary-rose-gold);">SEO & Tags</h5> <div class="row g-3"> <div class="col-md-12"> <div class="form-group-admin"> <label class="form-label-admin">Tags (comma separated)</label> <input type="text" name="tags" class="form-control-admin" value="<?= htmlspecialchars($product['tags'] ?? '') ?>"> </div> </div> <div class="col-md-12"> <div class="form-group-admin"> <label class="form-label-admin">Meta Title</label> <input type="text" name="meta_title" class="form-control-admin" value="<?= htmlspecialchars($product['meta_title'] ?? '') ?>"> </div> </div> <div class="col-md-12"> <div class="form-group-admin"> <label class="form-label-admin">Meta Description</label> <textarea name="meta_description" class="form-control-admin" rows="2"><?= htmlspecialchars($product['meta_description'] ?? '') ?></textarea> </div> </div> </div> <hr style="margin: 30px 0;"> <div class="d-flex gap-2"> <button type="submit" class="btn-admin btn-primary-admin"> <i class="fas fa-save"></i> Update Product </button> <a href="<?= ADMIN_PATH ?>pages/products.php" class="btn-admin btn-secondary-admin">Cancel</a> </div> </form> </div> <?php require_once '../includes/footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder