in accountant/managepayments.php (<?php sessionstart(); requireonce '../dbconnect.php';…
- User
in accountant/manage_payments.php (<?php session_start(); require_once '../db_connect.php';
// Check if the accountant is logged in if (!isset($_SESSION['accountant_id'])) { header("Location: login.php"); exit(); }
try { // Fetch payments along with patient name and invoice number pdo->prepare(" SELECT payments.*, patients.name AS patient_name, invoices.id AS invoice_number FROM payments JOIN patients ON payments.patient_id = patients.id JOIN invoices ON payments.invoice_id = invoices.id ORDER BY payment_date DESC "); payments = stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException e) { echo "Error fetching payments: " . $e->getMessage(); exit(); } ?>
/* Root Variables */ :root { --primary-color: #4F3F02; --secondary-color: #D1C287; --background-color: #F8F9FA; --card-background: #FFFFFF; --text-color: #333333; --font-family: 'Poppins', sans-serif; } body { background-color: var(--background-color); font-family: var(--font-family); color: var(--text-color); margin: 0; padding: 0; } .dashboard-header { background-color: var(--primary-color); color: #fff; padding: 20px; text-align: center; margin-bottom: 30px; } .dashboard-header h1 { font-weight: 700; margin-bottom: 10px; } .dashboard-header p a { color: #fff; margin: 0 10px; text-decoration: none; font-weight: 600; } .dashboard-header p a:hover { text-decoration: underline; } .btn-custom { background-color: var(--secondary-color); color: var(--primary-color); font-weight: 600; border: none; border-radius: 5px; transition: all 0.3s; padding: 10px 20px; } .btn-custom:hover { background-color: var(--primary-color); color: #fff; } .table thead { background-color: var(--secondary-color); color: var(--primary-color); font-weight: 600; } .table tbody tr:hover { background-color: rgba(209, 194, 135, 0.2); } .pagination .page-link { color: var(--primary-color); } .pagination .page-item.active .page-link { background-color: var(--secondary-color); border-color: var(--secondary-color); color: var(--primary-color); } .form-control, .form-select { border-radius: 5px; } .dataTables_wrapper .dataTables_length, .dataTables_wrapper .dataTables_filter { margin-bottom: 20px; } .dataTables_wrapper .dataTables_paginate .paginate_button { background-color: var(--secondary-color) !important; border: none !important; border-radius: 5px !important; color: var(--primary-color) !important; margin: 0 2px; padding: 5px 10px; } .dataTables_wrapper .dataTables_paginate .paginate_button:hover { background-color: var(--primary-color) !important; color: #fff !important; border: none; } .dataTables_wrapper .dataTables_paginate .paginate_button.current { background-color: var(--primary-color) !important; color: #fff !important; } .dataTables_wrapper .dataTables_info { margin-top: 10px; } /* Responsive Adjustments */ @media (max-width: 576px) { .dashboard-header h1 { font-size: 24px; } .table-responsive { overflow-x: auto; } } </style><!-- Main Container --> <div class="container"> <!-- Add New Payment Button --> <a href="create_payment.php" class="btn btn-custom mb-4">Add New Payment</a> <!-- Payment Table --> <div class="table-responsive"> <table class="table table-striped table-hover" id="paymentsTable"> <thead> <tr> <th>Payment ID</th> <th>Patient Name</th> <th>Invoice Number</th> <th>Amount Paid</th> <th>Payment Date</th> <th>Payment Method</th> <th>Notes</th> <th>Actions</th> <!-- Added Actions Column --> </tr> </thead> <tbody> <?php if (count($payments) > 0): ?> <?php foreach ($payments as $payment): ?> <tr> <td><?php echo htmlspecialchars($payment['id']); ?></td> <td><?php echo htmlspecialchars($payment['patient_name']); ?></td> <td><?php echo htmlspecialchars($payment['invoice_number']); ?></td> <td>$<?php echo number_format($payment['amount_paid'], 2); ?></td> <td><?php echo htmlspecialchars($payment['payment_date']); ?></td> <td><?php echo htmlspecialchars($payment['payment_method']); ?></td> <td><?php echo htmlspecialchars($payment['notes']); ?></td> <td> <a href="view_payment.php?id=<?php echo $payment['id']; ?>" class="btn btn-sm btn-info">View</a> <a href="edit_payment.php?id=<?php echo $payment['id']; ?>" class="btn btn-sm btn-primary">Edit</a> <form action="manage_payments.php" method="POST" style="display:inline-block;"> <input type="hidden" name="payment_id" value="<?php echo $payment['id']; ?>"> <button type="submit" name="delete_payment" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this payment?');">Delete</button> </form> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="7" class="text-center">No payments found.</td> </tr> <?php endif; ?> </tbody> </table> </div> </div> <!-- Bootstrap JS and dependencies --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- DataTables JS --> <script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.13.5/js/dataTables.bootstrap5.min.js"></script> <!-- Bootstrap JS --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <!-- Custom JS --> <script> $(document).ready(function() { $('#paymentsTable').DataTable({ "ordering": true, "order": [[0, "desc"]], "lengthChange": true, "lengthMenu": [10, 20, 50, 100], "pageLength": 20, "language": { "search": "", "searchPlaceholder": "Search payments...", "lengthMenu": "Show _MENU_ entries", "info": "Showing _START_ to _END_ of _TOTAL_ payments", }, "dom": '<"top"f><"table-responsive"tr><"bottom"lip><"clear">', }); }); </script> - Assistant
Sure! Below is a complete implementation for the
delete_payment.phpscript that deletes a payment from the database based on the provided payment ID.Create a file named
delete_payment.phpand add the following code:<?php session_start(); require_once '../db_connect.php'; // Check if the accountant is logged in if (!isset($_SESSION['accountant_id'])) { header("Location: login.php"); exit(); } // Check if the payment ID is set and is numeric if (isset($_POST['payment_id']) && is_numeric($_POST['payment_id'])) { $payment_id = $_POST['payment_id']; try { // Prepare SQL statement to delete the payment $stmt = $pdo->prepare("DELETE FROM payments WHERE id = :payment_id"); $stmt->bindParam(':payment_id', $payment_id, PDO::PARAM_INT); $stmt->execute(); // Redirect back to manage payments page with success message $_SESSION['message'] = "Payment deleted successfully."; header("Location: manage_payments.php"); exit(); } catch (PDOException $e) { // Redirect back to manage payments page with error message $_SESSION['error'] = "Error deleting payment: " . $e->getMessage(); header("Location: manage_payments.php"); exit(); } } else { // If payment ID is not valid, redirect to manage payments page $_SESSION['error'] = "Invalid payment ID."; header("Location: manage_payments.php"); exit(); } ?>Explanation of
delete_payment.php:-
Session Management: Just like in your original code, we use sessions to check if the accountant is logged in.
-
Payment ID Check: We check if a payment ID is set in the POST request and validate that it is numeric. If not valid, we redirect back with an error message.
-
Database Connection: It uses the required
db_connect.phpfile to connect to the database. -
Deleting the Payment: It prepares a SQL statement to delete the specified payment from the database using a prepared statement to prevent SQL injection.
-
Success and Error Handling: After successfully deleting a payment, it sets a success message in the session and redirects back to
manage_payments.php. If there’s an error, it sets an error message and redirects back. -
Redirects: All actions either redirect back to the
manage_payments.phppage or to a login page if the session has expired.
Integration With
manage_payments.php:Since you already have the form configured in your
manage_payments.phpfile, it is ready to utilize thedelete_payment.phpbecause your deletion form is already posting tomanage_payments.php. You can simply replace the form action URL withdelete_payment.phpas follows:<form action="delete_payment.php" method="POST" style="display:inline-block;"> <input type="hidden" name="payment_id" value="<?php echo $payment['id']; ?>"> <button type="submit" name="delete_payment" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this payment?');">Delete</button> </form>With this modification, the form will now use the
delete_payment.phpscript to handle deletion requests instead. Make sure to also handle session messages inmanage_payments.phpto display success or error messages to the accountant as follows:Handling Messages in
manage_payments.phpAdd the following block right after starting the
<body>tag to display feedback messages:<?php if (isset($_SESSION['message'])): ?> <div class="alert alert-success"> <?php echo htmlspecialchars($_SESSION['message']); unset($_SESSION['message']); // Clear the message after displaying it ?> </div> <?php endif; ?> <?php if (isset($_SESSION['error'])): ?> <div class="alert alert-danger"> <?php echo htmlspecialchars($_SESSION['error']); unset($_SESSION['error']); // Clear the error message after displaying it ?> </div> <?php endif; ?>This will allow the user to see any messages related to their actions when they return to the manage payments page.
-
- User
in accountant/manage_payments.php (<?php session_start(); require_once '../db_connect.php';
// Check if the accountant is logged in if (!isset($_SESSION['accountant_id'])) { header("Location: login.php"); exit(); }
try { // Fetch payments along with patient name and invoice number pdo->prepare(" SELECT payments.*, patients.name AS patient_name, invoices.id AS invoice_number FROM payments JOIN patients ON payments.patient_id = patients.id JOIN invoices ON payments.invoice_id = invoices.id ORDER BY payment_date DESC "); payments = stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException e) { echo "Error fetching payments: " . $e->getMessage(); exit(); } ?>
/* Root Variables */ :root { --primary-color: #4F3F02; --secondary-color: #D1C287; --background-color: #F8F9FA; --card-background: #FFFFFF; --text-color: #333333; --font-family: 'Poppins', sans-serif; } body { background-color: var(--background-color); font-family: var(--font-family); color: var(--text-color); margin: 0; padding: 0; } .dashboard-header { background-color: var(--primary-color); color: #fff; padding: 20px; text-align: center; margin-bottom: 30px; } .dashboard-header h1 { font-weight: 700; margin-bottom: 10px; } .dashboard-header p a { color: #fff; margin: 0 10px; text-decoration: none; font-weight: 600; } .dashboard-header p a:hover { text-decoration: underline; } .btn-custom { background-color: var(--secondary-color); color: var(--primary-color); font-weight: 600; border: none; border-radius: 5px; transition: all 0.3s; padding: 10px 20px; } .btn-custom:hover { background-color: var(--primary-color); color: #fff; } .table thead { background-color: var(--secondary-color); color: var(--primary-color); font-weight: 600; } .table tbody tr:hover { background-color: rgba(209, 194, 135, 0.2); } .pagination .page-link { color: var(--primary-color); } .pagination .page-item.active .page-link { background-color: var(--secondary-color); border-color: var(--secondary-color); color: var(--primary-color); } .form-control, .form-select { border-radius: 5px; } .dataTables_wrapper .dataTables_length, .dataTables_wrapper .dataTables_filter { margin-bottom: 20px; } .dataTables_wrapper .dataTables_paginate .paginate_button { background-color: var(--secondary-color) !important; border: none !important; border-radius: 5px !important; color: var(--primary-color) !important; margin: 0 2px; padding: 5px 10px; } .dataTables_wrapper .dataTables_paginate .paginate_button:hover { background-color: var(--primary-color) !important; color: #fff !important; border: none; } .dataTables_wrapper .dataTables_paginate .paginate_button.current { background-color: var(--primary-color) !important; color: #fff !important; } .dataTables_wrapper .dataTables_info { margin-top: 10px; } /* Responsive Adjustments */ @media (max-width: 576px) { .dashboard-header h1 { font-size: 24px; } .table-responsive { overflow-x: auto; } } </style><!-- Main Container --> <div class="container"> <!-- Add New Payment Button --> <a href="create_payment.php" class="btn btn-custom mb-4">Add New Payment</a> <!-- Payment Table --> <div class="table-responsive"> <table class="table table-striped table-hover" id="paymentsTable"> <thead> <tr> <th>Payment ID</th> <th>Patient Name</th> <th>Invoice Number</th> <th>Amount Paid</th> <th>Payment Date</th> <th>Payment Method</th> <th>Notes</th> <th>Actions</th> <!-- Added Actions Column --> </tr> </thead> <tbody> <?php if (count($payments) > 0): ?> <?php foreach ($payments as $payment): ?> <tr> <td><?php echo htmlspecialchars($payment['id']); ?></td> <td><?php echo htmlspecialchars($payment['patient_name']); ?></td> <td><?php echo htmlspecialchars($payment['invoice_number']); ?></td> <td>$<?php echo number_format($payment['amount_paid'], 2); ?></td> <td><?php echo htmlspecialchars($payment['payment_date']); ?></td> <td><?php echo htmlspecialchars($payment['payment_method']); ?></td> <td><?php echo htmlspecialchars($payment['notes']); ?></td> <td> <a href="view_payment.php?id=<?php echo $payment['id']; ?>" class="btn btn-sm btn-info">View</a> <a href="edit_payment.php?id=<?php echo $payment['id']; ?>" class="btn btn-sm btn-primary">Edit</a> <form action="manage_payments.php" method="POST" style="display:inline-block;"> <input type="hidden" name="payment_id" value="<?php echo $payment['id']; ?>"> <button type="submit" name="delete_payment" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this payment?');">Delete</button> </form> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="7" class="text-center">No payments found.</td> </tr> <?php endif; ?> </tbody> </table> </div> </div> <!-- Bootstrap JS and dependencies --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- DataTables JS --> <script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.13.5/js/dataTables.bootstrap5.min.js"></script> <!-- Bootstrap JS --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <!-- Custom JS --> <script> $(document).ready(function() { $('#paymentsTable').DataTable({ "ordering": true, "order": [[0, "desc"]], "lengthChange": true, "lengthMenu": [10, 20, 50, 100], "pageLength": 20, "language": { "search": "", "searchPlaceholder": "Search payments...", "lengthMenu": "Show _MENU_ entries", "info": "Showing _START_ to _END_ of _TOTAL_ payments", }, "dom": '<"top"f><"table-responsive"tr><"bottom"lip><"clear">', }); }); </script> - Assistant
Certainly! Below is the complete code for a new
delete_payment.phpfile that you can create to handle the deletion of a payment from the database. This script will process the deletion request when the corresponding "Delete" button is pressed in themanage_payments.php.Step 1: Create
delete_payment.phpCreate a file named
delete_payment.phpand add the following code:<?php session_start(); require_once '../db_connect.php'; // Check if the accountant is logged in if (!isset($_SESSION['accountant_id'])) { header("Location: login.php"); exit(); } // Check if the payment ID is set and is numeric if (isset($_POST['payment_id']) && is_numeric($_POST['payment_id'])) { $payment_id = $_POST['payment_id']; try { // Prepare SQL statement to delete the payment $stmt = $pdo->prepare("DELETE FROM payments WHERE id = :payment_id"); $stmt->bindParam(':payment_id', $payment_id, PDO::PARAM_INT); $stmt->execute(); // Redirect back to manage payments page with success message $_SESSION['message'] = "Payment deleted successfully."; header("Location: manage_payments.php"); exit(); } catch (PDOException $e) { // Redirect back to manage payments page with error message $_SESSION['error'] = "Error deleting payment: " . $e->getMessage(); header("Location: manage_payments.php"); exit(); } } else { // If payment ID is not valid, redirect to manage payments page $_SESSION['error'] = "Invalid payment ID."; header("Location: manage_payments.php"); exit(); } ?>Explanation of the Code:
-
Session Management: The script starts by managing sessions to check if the accountant is logged in.
-
Input Validation: It checks that the
payment_idis set in thePOSTrequest and verifies whether it is numeric to prevent SQL injection. -
Database Connection: The script requires the
db_connect.phpfile to establish a connection with the database. -
SQL Query Execution: It prepares an SQL statement to delete the specified payment and binds the parameter to prevent SQL injections. After executing the statement, it redirects back to
manage_payments.php, storing a success message in the session. -
Error Handling: If there is an error during the deletion, it catches the exception and redirects back with an error message. If the payment ID is invalid, it also redirects back with an error.
Step 2: Modify
manage_payments.phpfor DeletionTo utilize the new
delete_payment.php, you should change the form inmanage_payments.phpthat manages the deletion. Here’s how to do that:In
manage_payments.php, modify the deletion form section like this:<td> <a href="view_payment.php?id=<?php echo $payment['id']; ?>" class="btn btn-sm btn-info">View</a> <a href="edit_payment.php?id=<?php echo $payment['id']; ?>" class="btn btn-sm btn-primary">Edit</a> <form action="delete_payment.php" method="POST" style="display:inline-block;"> <input type="hidden" name="payment_id" value="<?php echo $payment['id']; ?>"> <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this payment?');">Delete</button> </form> </td>Step 3: Handle Messages in
manage_payments.phpEnsure that you add session messages handling to display success or error messages to users, as mentioned in the previous message. Here’s how to implement it:
Add this block at the very top of the
<body>inmanage_payments.php, just after the opening<body>tag:<?php if (isset($_SESSION['message'])): ?> <div class="alert alert-success"> <?php echo htmlspecialchars($_SESSION['message']); unset($_SESSION['message']); // Clear the message after displaying it ?> </div> <?php endif; ?> <?php if (isset($_SESSION['error'])): ?> <div class="alert alert-danger"> <?php echo htmlspecialchars($_SESSION['error']); unset($_SESSION['error']); // Clear the error message after displaying it ?> </div> <?php endif; ?>This will allow for a feedback loop where the accountant receives notifications of their actions when they return to the payments management page.
With these modifications, your code will be able to handle payment deletions effectively and provide user feedback accordingly!
-