home/abhiramc/public_html/acts.service/save_bank_details.php 0000644 00000012065 15021152026 0020302 0 ustar 00 getConnection();
// Check if the form data is posted via POST method
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Retrieve bank details from the POST data
$emplid = $_POST['emplid']; // use emplid directly from the request
$bankName = $_POST['bank_name'];
$holderName = $_POST['holder_name'];
$accountNumber = $_POST['account_number'];
$branchName = $_POST['branch_name'];
$ifscCode = $_POST['ifsc_code'];
$epfno = $_POST['epfno'];
$esino = $_POST['esino'];
// Check if any fields are empty
if (empty($emplid) || empty($bankName) || empty($holderName) || empty($accountNumber) || empty($branchName) || empty($ifscCode)) {
echo json_encode(['success' => false, 'message' => 'All fields are required.']);
exit;
}
// Step 1: Check if the record with the given emplid exists in the acts_emp_bank table
$queryCheck = "SELECT * FROM acts_emp_bank WHERE empid = :emplid";
// Prepare the statement to check for existing record
$stmtCheck = $db->prepare($queryCheck);
// Bind the parameters to the prepared statement
$stmtCheck->bindParam(':emplid', $emplid);
// Execute the check query
$stmtCheck->execute();
if ($stmtCheck->rowCount() > 0) {
// If the record exists, update the bank details
$queryUpdate = "UPDATE acts_emp_bank SET
bankaccountno = :account_number,
bankname = :bank_name,
bankacholdername = :holder_name,
bankbranchname = :branch_name,
bankifsccode = :ifsc_code,
epfno = :epfno,
esino = :esino
WHERE empid = :emplid";
// Prepare the statement for updating the record
$stmtUpdate = $db->prepare($queryUpdate);
// Bind the parameters to the prepared statement
$stmtUpdate->bindParam(':emplid', $emplid);
$stmtUpdate->bindParam(':bank_name', $bankName);
$stmtUpdate->bindParam(':holder_name', $holderName);
$stmtUpdate->bindParam(':branch_name', $branchName);
$stmtUpdate->bindParam(':ifsc_code', $ifscCode);
$stmtUpdate->bindParam(':epfno', $epfno);
$stmtUpdate->bindParam(':esino', $esino);
$stmtUpdate->bindParam(':account_number', $accountNumber);
// Execute the update query
if ($stmtUpdate->execute()) {
echo json_encode(['success' => true, 'message' => 'Bank details updated successfully!']);
} else {
echo json_encode(['success' => false, 'message' => 'Error updating bank details. Please try again.']);
}
} else {
// If the record does not exist, fetch the latest empid from acts_emp table for insertion
$queryLatestEmpId = "SELECT empid FROM acts_emp ORDER BY empid DESC LIMIT 1";
// Prepare the statement to fetch the latest empid
$stmtLatestEmpId = $db->prepare($queryLatestEmpId);
$stmtLatestEmpId->execute();
// Check if we fetched the latest empid
if ($stmtLatestEmpId->rowCount() > 0) {
$row = $stmtLatestEmpId->fetch(PDO::FETCH_ASSOC);
$empid = $row['empid']; // Get the latest empid
} else {
echo json_encode(['success' => false, 'message' => 'No employee records found.']);
exit;
}
// Step 2: Insert a new record with the latest empid
$queryInsert = "INSERT INTO acts_emp_bank (empid, bankname, bankacholdername, bankaccountno, bankbranchname, bankifsccode, epfno, esino)
VALUES (:empid, :bank_name, :holder_name, :account_number, :branch_name, :ifsc_code, :epfno, :esino)";
// Prepare the statement for inserting the record
$stmtInsert = $db->prepare($queryInsert);
// Bind the parameters to the prepared statement
$stmtInsert->bindParam(':empid', $empid); // Use the latest empid
$stmtInsert->bindParam(':bank_name', $bankName);
$stmtInsert->bindParam(':holder_name', $holderName);
$stmtInsert->bindParam(':account_number', $accountNumber);
$stmtInsert->bindParam(':branch_name', $branchName);
$stmtInsert->bindParam(':ifsc_code', $ifscCode);
$stmtInsert->bindParam(':epfno', $epfno);
$stmtInsert->bindParam(':esino', $esino);
// Execute the insert query
if ($stmtInsert->execute()) {
echo json_encode(['success' => true, 'message' => 'Bank details saved successfully!']);
} else {
echo json_encode(['success' => false, 'message' => 'Error saving bank details. Please try again.']);
}
}
} else {
// If the request method is not POST, return an error
echo json_encode(['success' => false, 'message' => 'Invalid request.']);
}
?>