<?php
 session_start();
 require_once('../connect.php');
 require_once('sql_required.php');
 

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve data from AJAX request
    $id = mysqli_real_escape_string($conn, $_POST['id']);
    $pres_reading = mysqli_real_escape_string($conn, $_POST['pres_reading']);
    $total_reading = mysqli_real_escape_string($conn, $_POST['total_reading']);
    $billing_amount = mysqli_real_escape_string($conn, $_POST['billing_amount']);
    $reading_date = mysqli_real_escape_string($conn, $_POST['reading_date']); // New Date Field

 // Ensure all required fields are provided
 if (empty($id) || empty($pres_reading) || empty($total_reading) || empty($billing_amount) || empty($reading_date)) {
    echo 'error';
    exit;
}

    // Update query
    $query = "UPDATE tbl_members_report 
              SET pres_reading = '$pres_reading', 
                  total_reading = '$total_reading', 
                  billing_amount = '$billing_amount' ,
                     reading_date = '$reading_date' 
              WHERE id = '$id'";

    // Execute query and check for success
    if (mysqli_query($conn, $query)) {
        echo 'success';
    } else {
        echo 'error';
    }

    // Close database connection
    mysqli_close($conn);
} else {
    echo 'invalid_request';
}
?>
