Skip to content

Commit

Permalink
transaction handling and rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
Shankar-Singh-Mahanty committed Jul 7, 2024
1 parent 1db4c96 commit 708f9d9
Showing 1 changed file with 59 additions and 33 deletions.
92 changes: 59 additions & 33 deletions deactivate-cug.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
<h2 class="heading">Deactivate CUG</h2>
</div>
<main class="main-content">
<form class="cug-form" method="POST" action="deactivate-cug.php">
<form class="cug-form" method="POST" action="">
<div class="form-group">
<label for="cugNo" class="cug-no-label">CUG NO</label>
<input type="text" id="cugNo" name="cugNo" class="cug-no-input" placeholder="Enter CUG No." required>
<button type="submit" class="submit-button">GO</button>
<button type="submit" name="search" class="submit-button">GO</button>
</div>
</form>
</main>
<?php

// Include database connection script
include 'db_connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$cug_no = null;
$message = null;

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['search'])) {
$cug_no = $_POST['cugNo'];

// Select the record to display it
Expand All @@ -51,30 +53,30 @@
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();

echo "<form method='POST' action='deactivate-cug.php'>";
echo "<form method='POST' action=''>";
echo "<input type='hidden' name='cugNo' value='$cug_no'>";
echo "<button type='submit' name='deactivate' class='deactivate-button'>Deactivate</button>";
echo "</form>";

echo "<table>
<thead>
<tr>
<th>CUG Number</th>
<th>Employee Number</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Unit</th>
<th>Department</th>
<th>Bill Unit No</th>
<th>Allocation</th>
<th>Operator</th>
<th>Plan</th>
<th>Status</th>
<th>Activate from</th>
<th>Inactive at</th>
</tr>
</thead>
<tbody>";
<thead>
<tr>
<th>CUG Number</th>
<th>Employee Number</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Unit</th>
<th>Department</th>
<th>Bill Unit No</th>
<th>Allocation</th>
<th>Operator</th>
<th>Plan</th>
<th>Status</th>
<th>Activate from</th>
<th>Inactive at</th>
</tr>
</thead>
<tbody>";
echo "<tr>";
echo "<td>" . $row["cug_number"] . "</td>";
echo "<td>" . $row["emp_number"] . "</td>";
Expand All @@ -90,11 +92,30 @@
echo "<td>" . $row["activate_from"] . "</td>";
echo "<td>" . $row["inactive_at"] . "</td>";
echo "</tr>
</tbody>
</table>";
</tbody>
</table>";
} else {
$message = "No records found";
}

$stmt->close();
}

// Check if deactivate button was clicked
if (isset($_POST['deactivate'])) {
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['deactivate'])) {
$cug_no = $_POST['cugNo'];

// Check if the status is already 'Inactive'
$select_status_sql = "SELECT status FROM cugdetails_transaction WHERE cug_number = ?";
$stmt_status = $conn->prepare($select_status_sql);
$stmt_status->bind_param("s", $cug_no);
$stmt_status->execute();
$result_status = $stmt_status->get_result();

if ($result_status->num_rows > 0) {
$row_status = $result_status->fetch_assoc();
if ($row_status['status'] == 'Inactive') {
$message = "CUG number $cug_no is already deactivated.";
} else {
// Begin transaction
$conn->begin_transaction();

Expand All @@ -113,27 +134,32 @@
if ($stmt_update->execute() && $stmt_delete->execute()) {
// Commit transaction
$conn->commit();
echo "<p class='session-message'>CUG number $cug_no deactivated and deleted successfully.</p>";
$message = "CUG number $cug_no deactivated and deleted successfully.";
} else {
// Rollback transaction if either statement fails
$conn->rollback();
echo "<p class='session-message'>Error deactivating and deleting CUG number.</p>";
$message = "Error deactivating and deleting CUG number.";
}

} catch (Exception $e) {
// Rollback transaction in case of error
$conn->rollback();
echo "<p class='session-message'>Error deactivating and deleting CUG number: " . $e->getMessage() . "</p>";
$message = "Error deactivating and deleting CUG number: " . $e->getMessage();
}
}

} else {
echo "<p>No records found</p>";
$message = "CUG number not found.";
}

$stmt->close();
$stmt_status->close();
}

// Display the message if available
if ($message) {
echo "<p class='session-message'>" . htmlspecialchars($message) . "</p>";
}

// Close the database connection
$conn->close();
?>
</body>
Expand Down

0 comments on commit 708f9d9

Please sign in to comment.