false, 'message' => 'User not logged in']); exit; } try { // Begin transaction $conn->beginTransaction(); // Get all unpaid fines for the current user $getFines = "SELECT fineid, fineamount FROM fine WHERE status = 'Unpaid' AND loanid IN (SELECT loanid FROM loan WHERE memberid = :userid)"; $stmt = $conn->prepare($getFines); $stmt->bindParam(':userid', $_SESSION['userid'], PDO::PARAM_INT); $stmt->execute(); $unpaidFines = $stmt->fetchAll(PDO::FETCH_ASSOC); if (empty($unpaidFines)) { throw new Exception('No unpaid fines found'); } // Process each unpaid fine foreach ($unpaidFines as $fine) { // Insert payment record $insertPayment = "INSERT INTO finepayment (paymentdate, paymentamount, fineid) VALUES (CURRENT_TIMESTAMP, :amount, :fineid)"; $stmt = $conn->prepare($insertPayment); $stmt->bindParam(':amount', $fine['fineamount'], PDO::PARAM_STR); $stmt->bindParam(':fineid', $fine['fineid'], PDO::PARAM_INT); $stmt->execute(); // Update fine status $updateFine = "UPDATE fine SET status = 'Paid', finedate = CURRENT_TIMESTAMP WHERE fineid = :fineid"; $stmt = $conn->prepare($updateFine); $stmt->bindParam(':fineid', $fine['fineid'], PDO::PARAM_INT); $stmt->execute(); } // Commit transaction $conn->commit(); echo json_encode([ 'success' => true, 'message' => 'All fines paid successfully' ]); } catch (Exception $e) { // Rollback transaction on error $conn->rollBack(); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); } ?>