<?php
session_start();

// 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
$username = $_POST['username'];
$password = $_POST['password'];

// Mencari pengguna di database
$sql = "SELECT * FROM users WHERE username = ? AND password = ?"; // Cek username dan password
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $username, $password); // Bind username dan password
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    // Set session
    $_SESSION['username'] = $row['username'];
    header("Location: index.php"); // Redirect ke halaman index
    exit();
} else {
    echo "Username atau password salah!";
}

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