MINI
C:
/
xampp
/
htdocs
/
voucher_app
/
Upload File:
files >> C:/xampp/htdocs/voucher_app/proses.php
<?php // Konfigurasi Database define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', ''); define('DB_NAME', 'voucher_db'); // Membuat koneksi $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); // Cek koneksi if ($conn->connect_error) { die("Koneksi gagal: " . $conn->connect_error); } // Set header untuk JSON header('Content-Type: application/json'); // Ambil dan sanitasi input $user = $conn->real_escape_string($_POST['user'] ?? ''); $voucher = $conn->real_escape_string($_POST['voucher'] ?? ''); $amount = floatval($_POST['amount'] ?? 0); $type = in_array($_POST['type'] ?? '', ['in', 'out']) ? $_POST['type'] : ''; // Validasi input $errors = []; if(empty($user)) $errors[] = "Nama pengguna harus diisi"; if(empty($voucher)) $errors[] = "Kode voucher harus diisi"; if($amount <= 0) $errors[] = "Jumlah saldo harus lebih dari 0"; if(empty($type)) $errors[] = "Tipe transaksi harus dipilih"; if(!empty($errors)) { header("Location: index.php?error=" . urlencode(implode(", ", $errors))); exit(); } try { // Mulai transaksi $conn->begin_transaction(); // Siapkan dan jalankan query $stmt = $conn->prepare("INSERT INTO transactions (user, voucher, amount, type) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssds", $user, $voucher, $amount, $type); if ($stmt->execute()) { $conn->commit(); header("Location: index.php?success=1"); } else { throw new Exception("Gagal menyimpan transaksi: " . $stmt->error); } } catch (Exception $e) { $conn->rollback(); header("Location: index.php?error=" . urlencode($e->getMessage())); } finally { $stmt->close(); $conn->close(); }
Copyright ©2021 || Defacer Indonesia