real_escape_string($_GET['id']); // Fetch sale data $query = "SELECT s.*, b.name as buyer_name FROM sales s JOIN buyers b ON s.buyer_id = b.id WHERE s.id = $sale_id"; $result = $conn->query($query); $sale = $result->fetch_assoc(); if (!$sale) { header("Location: sales.php"); exit(); } // Fetch buyers for dropdown $query = "SELECT id, name FROM buyers ORDER BY name"; $result = $conn->query($query); $buyers = $result->fetch_all(MYSQLI_ASSOC); if ($_SERVER["REQUEST_METHOD"] == "POST") { $date = $conn->real_escape_string($_POST['date']); $buyer_id = $conn->real_escape_string($_POST['buyer_id']); $weight_tilapia = $conn->real_escape_string($_POST['weight_tilapia']); $weight_small_fish = $conn->real_escape_string($_POST['weight_small_fish']); $weight_big_fish = $conn->real_escape_string($_POST['weight_big_fish']); $include_delivery = isset($_POST['include_delivery']) ? 1 : 0; $final_amount = $conn->real_escape_string($_POST['final_amount']); $harvesting_charges = $conn->real_escape_string($_POST['harvesting_charges']); $query = "UPDATE sales SET date = '$date', buyer_id = $buyer_id, weight_tilapia = $weight_tilapia, weight_small_fish = $weight_small_fish, weight_big_fish = $weight_big_fish, include_delivery = $include_delivery, final_amount = $final_amount, harvesting_charges = $harvesting_charges WHERE id = $sale_id"; if ($conn->query($query) === TRUE) { $_SESSION['success'] = "Sale updated successfully."; header("Location: sales.php"); exit(); } else { $error = "Error updating sale: " . $conn->error; } } // Generate buyer options $buyerOptions = ''; foreach ($buyers as $buyer) { $selected = ($buyer['id'] == $sale['buyer_id']) ? 'selected' : ''; $buyerOptions .= ""; } $includeDeliveryChecked = $sale['include_delivery'] ? 'checked' : ''; $content = <<Edit Sale
{$sale['final_amount']}
HTML; include 'main_layout.php'; ?>