:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/demo/jdc/new/admin/**
:: Editing File: product_delete.php
<?php // product_delete.php require_once 'db.php'; require_once 'session.php'; // 1️⃣ Validate Product ID if (!isset($_GET['id']) || empty($_GET['id'])) { $_SESSION['error'] = "Error: Product ID is missing for deletion."; header("Location: product.php"); exit; } $product_id = (int)$_GET['id']; $success = false; $product_name = "Unknown Product"; // Image folder $upload_dir = '../uploads/products/'; // Start transaction mysqli_begin_transaction($conn); try { // 2️⃣ Fetch Product Name and Image $old_image = null; $sql_fetch = "SELECT name, image FROM products WHERE id = ?"; $stmt_fetch = mysqli_prepare($conn, $sql_fetch); if ($stmt_fetch) { mysqli_stmt_bind_param($stmt_fetch, "i", $product_id); mysqli_stmt_execute($stmt_fetch); mysqli_stmt_bind_result($stmt_fetch, $product_name, $old_image); mysqli_stmt_fetch($stmt_fetch); mysqli_stmt_close($stmt_fetch); } // 3️⃣ Delete Product $sql_delete = "DELETE FROM products WHERE id = ?"; $stmt_delete = mysqli_prepare($conn, $sql_delete); if ($stmt_delete) { mysqli_stmt_bind_param($stmt_delete, "i", $product_id); if (mysqli_stmt_execute($stmt_delete)) { mysqli_commit($conn); $success = true; mysqli_stmt_close($stmt_delete); } else { throw new Exception("Database execution error: " . mysqli_stmt_error($stmt_delete)); } } else { throw new Exception("Database preparation error: " . mysqli_error($conn)); } } catch (Exception $e) { mysqli_rollback($conn); $_SESSION['error'] = "Failed to delete product: " . $e->getMessage(); header("Location: product.php"); exit; } // 4️⃣ Delete Image File (after DB success) if ($success) { if (!empty($old_image)) { $image_path = $upload_dir . basename($old_image); if (file_exists($image_path) && is_file($image_path)) { unlink($image_path); } } $_SESSION['success'] = "Product **'{$product_name}'** (ID: {$product_id}) deleted successfully 🗑️"; header("Location: product.php"); exit; } mysqli_close($conn); ?>