set_charset("utf8mb4"); // ================= AUTO RECEIPT CODE GENERATION ================= function generateReceiptCode($conn, $taluka, $month) { $prefix = strtoupper($taluka) . "/" . strtoupper($month) . "/"; $searchPrefix = $prefix . "%"; // SECURE SELECT: Using prepared statements $stmt = $conn->prepare("SELECT receipt_code FROM receipts WHERE receipt_code LIKE ? ORDER BY receipt_code DESC LIMIT 1"); $stmt->bind_param("s", $searchPrefix); $stmt->execute(); $result = $stmt->get_result(); if ($row = $result->fetch_assoc()) { $lastNumber = intval(substr($row['receipt_code'], -4)); $newNumber = str_pad($lastNumber + 1, 4, "0", STR_PAD_LEFT); } else { $newNumber = "0001"; } $stmt->close(); return $prefix . $newNumber; } // Example variables (Ensure these are set from your form/session) $taluka = $_POST['taluka'] ?? ''; $month = $_POST['month'] ?? ''; // ================= FETCH ESTABLISHMENTS ================= $estList = []; if (!empty($taluka)) { $stmt = $conn->prepare("SELECT est_name FROM establishments WHERE taluka = ?"); $stmt->bind_param("s", $taluka); $stmt->execute(); $res = $stmt->get_result(); while ($row = $res->fetch_assoc()) { $estList[] = $row['est_name']; } $stmt->close(); } // ================= SAVE DATA (TRANSACTIONAL & SECURE) ================= if ($_SERVER['REQUEST_METHOD'] == 'POST') { $receipt_code = $_POST['receipt_code']; $balance = $_POST['balance_amount']; $amount = $_POST['total_amount'] ?? 0; $date = $_POST['date'] ?? date('Y-m-d'); if ($balance != 0) { die(""); } // START TRANSACTION: Security for data integrity $conn->begin_transaction(); try { // 1. Insert Main Receipt $stmt1 = $conn->prepare("INSERT INTO receipts (receipt_code, taluka, month, total_amount, date) VALUES (?, ?, ?, ?, ?)"); $stmt1->bind_param("sssds", $receipt_code, $taluka, $month, $amount, $date); $stmt1->execute(); $stmt1->close(); // 2. Insert Details Loop $est_names = $_POST['est_name']; $row_amount = $_POST['row_amount']; $row_text = $_POST['row_text']; $stmt2 = $conn->prepare("INSERT INTO receipt_details (receipt_code, est_name, amount, remarks) VALUES (?, ?, ?, ?)"); // We bind to variables that change inside the loop $stmt2->bind_param("ssds", $receipt_code, $curr_est, $curr_amt, $curr_rem); for ($i = 0; $i < count($est_names); $i++) { $curr_est = $est_names[$i]; $curr_amt = $row_amount[$i]; $curr_rem = $row_text[$i]; $stmt2->execute(); } $stmt2->close(); // COMMIT: Save everything to the DB $conn->commit(); echo ""; exit; } catch (Exception $e) { $conn->rollback(); // Cancel everything if any query fails throw $e; } } } catch (Exception $e) { error_log($e->getMessage()); exit('A database error occurred. Please try again later.'); } ?> Establishment Amount Entry

Establishment Amount Entry

Sr No Taluka Establishment Amount Remarks Action

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

1 thought on “Hello world!”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top