<?php
require 'db_connection.php';

$student_id = $_POST['student_id'];
$amount = $_POST['amount'];

$conn->begin_transaction();

try{

$conn->query("
UPDATE students 
SET fees_paid = fees_paid + $amount,
fees_outstanding = fees_outstanding - $amount
WHERE student_id = $student_id
");

$receipt_no = "RCPT".time();

$conn->query("
INSERT INTO receipts(student_id, receipt_no, date)
VALUES($student_id,'$receipt_no',NOW())
");

$conn->commit();

echo "Payment Successful";

}catch(Exception $e){
$conn->rollback();
echo "Payment Failed";
}