prepare("SELECT * FROM sales WHERE id = ?"); $stmt->bind_param("i", $saleId); $stmt->execute(); $result = $stmt->get_result(); $sale = $result->fetch_assoc(); $stmt->close(); if (!$sale) { $_SESSION['error'] = "Sale not found."; header("Location: sales.php"); exit(); } $buyersQuery = "SELECT id, name FROM buyers ORDER BY name"; $buyers = $conn->query($buyersQuery)->fetch_all(MYSQLI_ASSOC); if ($_SERVER["REQUEST_METHOD"] == "POST") { try { $stmt = $conn->prepare("UPDATE sales SET date = ?, buyer_id = ?, weight_tilapia = ?, weight_small_fish = ?, weight_big_fish = ?, include_delivery = ?, final_amount = ?, harvesting_charges = ? WHERE id = ?"); $stmt->bind_param("sidddiddi", $_POST['date'], $_POST['buyer_id'], $_POST['weight_tilapia'], $_POST['weight_small_fish'], $_POST['weight_big_fish'], isset($_POST['include_delivery']) ? 1 : 0, $_POST['final_amount'], $_POST['harvesting_charges'], $saleId ); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Sale updated successfully!"; header("Location: sales.php"); exit(); } catch (Exception $e) { $_SESSION['error'] = "Error updating sale."; error_log("Edit sale error: " . $e->getMessage()); } } $buyerOptions = ''; foreach ($buyers as $buyer) { $selected = $buyer['id'] == $sale['buyer_id'] ? 'selected' : ''; $buyerOptions .= ""; } $includeDeliveryChecked = $sale['include_delivery'] ? 'checked' : ''; $content = <<

Edit Sale

Cancel
HTML; include 'main_layout.php'; $conn->close(); ?>