How to develop Projects(HMS) Using PHP and Mysql
Part 11
Billing Part
billing.php
<?php
include '../../db/db.php'; // Database connection
if (isset($_GET['id'])) {
$appointment_id = intval($_GET['id']);
// Fetch appointment details
$query = "SELECT a.*, p.name AS patient_name, d.name AS doctor_name
FROM appointments a
JOIN patients p ON a.patient_id = p.id
JOIN doctors d ON a.doctor_id = d.id
WHERE a.id = $appointment_id";
$result = $conn->query($query);
if ($result->num_rows > 0) {
$appointment = $result->fetch_assoc();
} else {
die("Invalid Appointment ID");
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['confirm_payment'])) {
$payment_method = $conn->real_escape_string($_POST['payment_method']);
$amount = floatval($_POST['amount']);
$transaction_id = $conn->real_escape_string($_POST['transaction_id']);
// Update payment status in `appointments` table
$update_query = "UPDATE appointments SET payment_status = 'Paid' WHERE id = $appointment_id";
if ($conn->query($update_query) === TRUE) {
// Redirect to receipt page with appointment ID
echo "<script>window.location='billing_receipt.php?id=$appointment_id';</script>";
exit;
} else {
echo "Error updating record: " . $conn->error;
}
}
// Handle Payment Confirmation
// if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['confirm_payment'])) {
// $payment_method = $conn->real_escape_string($_POST['payment_method']);
// $amount = floatval($_POST['amount']);
// $transaction_id = $conn->real_escape_string($_POST['transaction_id']);
// // Update payment status in `appointments` table (OR insert into `payments` table)
// $update_query = "UPDATE appointments SET payment_status = 'Paid' WHERE id = $appointment_id";
// if ($conn->query($update_query) === TRUE) {
// echo "<script>alert('Payment Confirmed!'); window.location='view_appointments.php';</script>";
// } else {
// echo "Error updating record: " . $conn->error;
// }
// }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Billing & Payment</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
<h2 class="text-center">💳 Billing for Appointment #<?= $appointment_id; ?></h2>
<div class="card p-4 shadow-lg">
<p><strong>Patient Name:</strong> <?= $appointment['patient_name']; ?></p>
<p><strong>Doctor:</strong> <?= $appointment['doctor_name']; ?></p>
<p><strong>Date:</strong> <?= $appointment['appointment_date']; ?></p>
<p><strong>Time:</strong> <?= $appointment['appointment_time']; ?></p>
<form method="post">
<div class="mb-3">
<label class="form-label">Payment Method</label>
<select name="payment_method" class="form-control" required>
<option value="Cash">Cash</option>
<option value="Credit Card">Credit Card</option>
<option value="Online Transfer">Online Transfer</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Amount</label>
<input type="number" name="amount" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Transaction ID (if applicable)</label>
<input type="text" name="transaction_id" class="form-control">
</div>
<button type="submit" name="confirm_payment" class="btn btn-success">Confirm Payment</button>
<a href="view_appointments.php" class="btn btn-secondary">Back</a>
</form>
</div>
</div>
</body>
</html>
billing_receipt.php
<?php
include '../../db/db.php'; // Database connection
if (!isset($_GET['id'])) {
die("Invalid request");
}
$appointment_id = intval($_GET['id']);
// Fetch appointment and payment details
$query = "SELECT a.*, p.name AS patient_name, d.name AS doctor_name, d.specialty
FROM appointments a
JOIN patients p ON a.patient_id = p.id
JOIN doctors d ON a.doctor_id = d.id
WHERE a.id = $appointment_id";
$result = $conn->query($query);
if ($result->num_rows == 0) {
die("Invalid Appointment ID");
}
$appointment = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Payment Receipt</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<style>
.receipt {
max-width: 600px;
margin: 50px auto;
padding: 20px;
border: 2px solid #ddd;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
.receipt-header {
text-align: center;
font-size: 20px;
font-weight: bold;
}
.receipt-footer {
text-align: center;
margin-top: 20px;
font-size: 14px;
}
.print-btn {
margin-top: 20px;
display: flex;
justify-content: space-between;
}
</style>
</head>
<body>
<div class="receipt">
<div class="receipt-header">
🏥 <strong>Hospital Management System</strong> 🏥
<hr>
</div>
<p><strong>Patient Name:</strong> <?= $appointment['patient_name']; ?></p>
<p><strong>Doctor:</strong> <?= $appointment['doctor_name']; ?> (<?= $appointment['specialty']; ?>)</p>
<p><strong>Appointment Date:</strong> <?= $appointment['appointment_date']; ?></p>
<p><strong>Appointment Time:</strong> <?= $appointment['appointment_time']; ?></p>
<p><strong>Payment Status:</strong> <span class="badge bg-success">Paid</span></p>
<div class="receipt-footer">
<hr>
<p><strong>Thank you for your payment!</strong></p>
<p>For any queries, contact our helpdesk.</p>
</div>
<div class="print-btn">
<button onclick="window.print()" class="btn btn-primary">🖨️ Print Receipt</button>
<a href="view_appointments.php" class="btn btn-secondary">🔙 Back</a>
</div>
</div>
</body>
</html>