<?php
// Koneksi ke database
$servername = "localhost"; // Ganti jika perlu
$username = "root"; // Ganti jika perlu
$password = ""; // Ganti jika perlu
$dbname = "bprkredit"; // Nama database yang telah dibuat

// Membuat koneksi
$conn = new mysqli($servername, $username, $password, $dbname);

// Cek koneksi
if ($conn->connect_error) {
    die("Koneksi gagal: " . $conn->connect_error);
}

// Mengambil data dari form
$id = $_POST['id'];
$nama = $_POST['nama'];
$ktp = $_POST['ktp'];
$alamat = $_POST['alamat'];
$kodepos = $_POST['kodepos'];
$telp = $_POST['telp'];
$nominal = $_POST['nominal'];

// Proses upload file jika ada
$upload_file = null;
if (!empty($_FILES['upload']['name'])) {
    $upload_dir = 'uploads/';
    $upload_file = $upload_dir . basename($_FILES['upload']['name']);
    move_uploaded_file($_FILES['upload']['tmp_name'], $upload_file);
}

// Menyimpan data yang diperbarui ke database
if ($upload_file) {
    // Jika ada file yang diupload
    $sql = "UPDATE pengajuan SET nama = ?, ktp = ?, alamat = ?, kodepos = ?, telp = ?, nominal = ?, upload = ? WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssssssi", $nama, $ktp, $alamat, $kodepos, $telp, $nominal, $upload_file, $id);
} else {
    // Jika tidak ada file yang diupload
    $sql = "UPDATE pengajuan SET nama = ?, ktp = ?, alamat = ?, kodepos = ?, telp = ?, nominal = ? WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssssssi", $nama, $ktp, $alamat, $kodepos, $telp, $nominal, $id);
}

if ($stmt->execute()) {
    echo "Pengajuan berhasil diperbarui!";
} else {
    echo "Error: " . $stmt->error;
}

// Menutup koneksi
$stmt->close();
$conn->close();
?>