<?php  
session_start();
include './../koneksi.php'; 
include './../cek_login.php';    

// uncomment the following line for debugging
// ini_set('display_errors', 1);
// error_reporting(E_ALL);

if (isset($_POST['id']) && isset($_POST['no'])) {
    
    $checked_array = $_POST['no'];
    
    foreach ($_POST['id'] as $key => $value) {
        if (in_array($_POST['id'][$key], $checked_array)) {
            $rekening = $_POST['rekening'][$key];           
            $tglmasehi = $_POST['tglmasehi'][$key];
            $tgl = date('Y-m-d');            
            $username = $_SESSION['username'];
            $keterangan = "Bukutabungan [".$rekening."]";
            
            // Prepare the statement
            $stmt = $koneksi->prepare("INSERT INTO bukutabungan (no, tgl, tglmasehi, rekening, kodetab, asuransi, blokir, tutup, keterangan, username) VALUES (NULL, ?, ?, ?, '11', 'T', '0', '0', ?, ?)");
            
            if ($stmt) {
                // Bind parameters
                $stmt->bind_param("sssss", $tgl, $tglmasehi, $rekening, $keterangan, $username);
                
                // Execute statement
                if ($stmt->execute()) {
                    echo "Data Berhasil Disimpan";
                } else {
                    echo "Error: " . $stmt->error;
                }
                
                $stmt->close();
            } else {
                die("Prepare failed: (" . $koneksi->errno . ") " . $koneksi->error);
            }
        }
    }
} else {
    echo "ID atau No tidak ditemukan.";
}
?>
