87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
session_start();
|
|
include 'config/database.php';
|
|
|
|
// Enable error reporting for debugging
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
|
|
// Pastikan koneksi ke database berhasil
|
|
if (!$conn) {
|
|
die("Koneksi database gagal: " . mysqli_connect_error());
|
|
}
|
|
|
|
// Tangani pengiriman form
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$nama_donatur = htmlspecialchars($_POST['nama_donatur']);
|
|
$jumlah_donasi = htmlspecialchars($_POST['jumlah_donasi']);
|
|
$donasi_metode = htmlspecialchars($_POST['metode_pembayaran']);
|
|
$keterangan = htmlspecialchars($_POST['keterangan']);
|
|
$status = 'Pending';
|
|
|
|
// Tangani upload file
|
|
$bukti_donasi = '';
|
|
if (isset($_FILES['bukti_donasi']) && $_FILES['bukti_donasi']['error'] == 0) {
|
|
$uploadDir = __DIR__ . '/assets/img/donasi/';
|
|
$fileTmpPath = $_FILES['bukti_donasi']['tmp_name'];
|
|
$fileName = time() . '_' . basename($_FILES['bukti_donasi']['name']);
|
|
$filePath = $uploadDir . $fileName;
|
|
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0755, true);
|
|
}
|
|
|
|
// Validasi format gambar
|
|
$allowedTypes = ['image/jpeg', 'image/png'];
|
|
$fileType = mime_content_type($fileTmpPath);
|
|
|
|
if (!in_array($fileType, $allowedTypes)) {
|
|
echo "Format bukti donasi harus JPG atau PNG.";
|
|
exit;
|
|
}
|
|
|
|
// Validasi ukuran gambar (maksimal 2MB)
|
|
if ($_FILES['bukti_donasi']['size'] > 2 * 1024 * 1024) {
|
|
echo "Ukuran file tidak boleh lebih dari 2MB.";
|
|
exit;
|
|
}
|
|
|
|
if (move_uploaded_file($fileTmpPath, $filePath)) {
|
|
$bukti_donasi = $fileName;
|
|
} else {
|
|
echo "Terjadi kesalahan saat mengupload file.";
|
|
exit;
|
|
}
|
|
} else {
|
|
echo "Tidak ada file yang diupload atau terjadi kesalahan.";
|
|
exit;
|
|
}
|
|
|
|
// Simpan ke database
|
|
$query = "INSERT INTO donasi (donasi_nama, donasi_jumlah, donasi_metode, donasi_keterangan, donasi_gambar, status)
|
|
VALUES (?, ?, ?, ?, ?, ?)";
|
|
|
|
$stmt = $conn->prepare($query);
|
|
if (!$stmt) {
|
|
die("Error dalam query: " . $conn->error);
|
|
}
|
|
|
|
$stmt->bind_param("sissss", $nama_donatur, $jumlah_donasi, $donasi_metode, $keterangan, $bukti_donasi, $status);
|
|
|
|
if ($stmt->execute()) {
|
|
// Ambil ID donasi terakhir yang baru dimasukkan
|
|
$donasi_id = $stmt->insert_id;
|
|
|
|
$_SESSION['message'] = "Donasi berhasil dilakukan.";
|
|
// Redirect ke index.php dengan ID donasi
|
|
header("Location: index.php?donasi_id=" . $donasi_id);
|
|
exit();
|
|
} else {
|
|
echo "Error: " . $stmt->error;
|
|
}
|
|
|
|
$stmt->close();
|
|
$conn->close();
|
|
}
|
|
?>
|