:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/crm/quatation/**
:: Editing File: delete_outward.php
<?php // delete_outward.php error_reporting(E_ALL); ini_set('display_errors', '1'); include_once 'session.php'; include 'db.php'; if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { $_SESSION['error'] = "Invalid request: No valid Outward ID provided."; header("Location: outward_list.php"); exit; } $outward_id = (int)$_GET['id']; mysqli_begin_transaction($conn); try { // 1) Get the receipt file path (if any) so we can delete the physical file after successful DB delete $sql_select = "SELECT outward_receipt FROM outward WHERE id = ? LIMIT 1"; $stmt = mysqli_prepare($conn, $sql_select); if (!$stmt) throw new Exception("Prepare failed: " . mysqli_error($conn)); mysqli_stmt_bind_param($stmt, 'i', $outward_id); mysqli_stmt_execute($stmt); $res = mysqli_stmt_get_result($stmt); $receipt_path = null; if ($row = mysqli_fetch_assoc($res)) { $receipt_path = $row['outward_receipt']; } else { // No row found — nothing to delete (treat as success or error based on your app) throw new Exception("Record not found."); } mysqli_stmt_close($stmt); // 2) Delete child rows in outward_box (if any) $sql_del_boxes = "DELETE FROM outward_box WHERE challan_id = ?"; $stmt = mysqli_prepare($conn, $sql_del_boxes); if (!$stmt) throw new Exception("Prepare failed (box delete): " . mysqli_error($conn)); mysqli_stmt_bind_param($stmt, 'i', $outward_id); if (!mysqli_stmt_execute($stmt)) { throw new Exception("Failed to delete outward_box: " . mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); // 3) Delete the main outward row $sql_del_outward = "DELETE FROM outward WHERE id = ? LIMIT 1"; $stmt = mysqli_prepare($conn, $sql_del_outward); if (!$stmt) throw new Exception("Prepare failed (outward delete): " . mysqli_error($conn)); mysqli_stmt_bind_param($stmt, 'i', $outward_id); if (!mysqli_stmt_execute($stmt)) { throw new Exception("Failed to delete outward: " . mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); // 4) Commit DB transaction mysqli_commit($conn); // 5) Delete the physical file (outside transaction) if (!empty($receipt_path)) { // Make safe absolute path for deletion $full_path = __DIR__ . '/' . ltrim($receipt_path, '/'); if (file_exists($full_path) && is_file($full_path)) { @unlink($full_path); } } $_SESSION['success'] = "Outward entry deleted successfully."; header("Location: outward_list.php"); exit; } catch (Exception $e) { mysqli_rollback($conn); $_SESSION['error'] = "Error deleting Outward entry: " . $e->getMessage(); header("Location: outward_list.php"); exit; }