delete function

main
Suvodip 2025-03-19 14:08:13 +05:30
parent 3d7ac1ba3b
commit 97d120a1b5
1 changed files with 53 additions and 4 deletions

View File

@ -1,6 +1,47 @@
<?php
require('../.hta_config/conf.php');
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete'])) {
if (!empty($_POST['customerId']) && !empty($_POST['invoiceId'])) {
$customerId = $_POST['customerId'];
$invoiceId = $_POST['invoiceId'];
try {
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Start Transaction
$db->beginTransaction();
// Delete from emi table
$stmt = $db->prepare("DELETE FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt->execute(['customerId' => $customerId, 'invoiceId' => $invoiceId]);
// Delete from invoice table
$stmt = $db->prepare("DELETE FROM invoice WHERE customerId = :customerId AND invoiceId = :invoiceId");
$stmt->execute(['customerId' => $customerId, 'invoiceId' => $invoiceId]);
// Delete from customers table (only if no dependent records exist)
$stmt = $db->prepare("DELETE FROM customers WHERE customerId = :customerId");
$stmt->execute(['customerId' => $customerId]);
// Commit Transaction
$db->commit();
echo '<div class="alert alert-success">Customer deleted successfully!</div>';
} catch (Exception $e) {
$db->rollBack();
error_log("Delete Error: " . $e->getMessage()); // Logs error without exposing details
echo '<div class="alert alert-danger">Error: ' . htmlspecialchars($e->getMessage()) . '</div>';
}
} else {
echo '<div class="alert alert-warning">Invalid input! Please check your data.</div>';
}
}
?>
<div class="container-xxl mt-4">
<h3 class="mb-3 ">Customer List</h3><hr>
<div class="table-responsive">
@ -21,10 +62,8 @@
<tbody>
<?php
try {
// Connect to the database
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Fetch customer data
$stmt = $db->prepare("SELECT * FROM customers ORDER BY regDate DESC");
$stmt->execute();
@ -83,10 +122,15 @@
<td>$<?php echo isset($emi['emiAmount']) ? htmlspecialchars($emi['emiAmount']) : '0.00'; ?></td>
<td>$<?php echo isset($emi['totalAmount']) ? htmlspecialchars($emi['totalAmount']) : '0.00'; ?></td>
<td class="d-flex gap-1">
<td class="d-flex flex-wrap gap-1">
<a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm" style="font-size: 13px;">New</a>
<a href="/customers/edit-invoice/?customerId=<?php echo $customer['customerId'].'&invoiceId='.$invoice['invoiceId']; ?>" class="btn btn-primary btn-sm" style="font-size: 13px;">Edit</a>
<a href="/customers/print-invoice/?customerId=<?php echo $customer['customerId'].'&invoiceId='.$invoice['invoiceId']; ?>" class="btn btn-primary btn-sm" style="font-size: 13px;">Print</a>
<form method="post">
<input type="hidden" name="customerId" value="<?php echo htmlspecialchars($customer['customerId']); ?>" />
<input type="hidden" name="invoiceId" value="<?php echo htmlspecialchars($invoice['invoiceId']); ?>" />
<button type="submit" name="delete" class="btn btn-danger btn-sm" style="font-size: 13px;">Delete</button>
</form>
</td>
</tr>
<?php
@ -98,8 +142,13 @@
<td><?php echo $customerSerial++; ?></td>
<td><?php echo htmlspecialchars($customer['name']); ?></td>
<td colspan="6" class="text-center">No EMI or Invoice Data</td>
<td>
<td class="d-flex flex-wrap gap-1">
<a href="/customers/generate-invoice/?customerId=<?php echo $customer['customerId']; ?>" class="btn btn-primary btn-sm">New</a>
<form method="post">
<input type="hidden" name="customerId" value="<?php echo htmlspecialchars($customer['customerId']); ?>" />
<input type="hidden" name="invoiceId" value="<?php echo htmlspecialchars($invoice['invoiceId']); ?>" />
<button type="submit" name="delete" class="btn btn-danger btn-sm" style="font-size: 13px;">Delete</button>
</form>
</td>
</tr>
<?php