<?php

require_once 'connection.php'; // Include your main functions file

if (isset($_POST['login'])) {
    $user_id = $_POST['user_id'];
    $password = $_POST['password'];
    
    // Direct database query for login
    global $conn;
    
    // Use prepared statement for security
    $stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE user_id = ? AND password = ?");
    mysqli_stmt_bind_param($stmt, "ss", $user_id, $password);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    if (mysqli_num_rows($result) == 1) {
        $_SESSION['session_id'] = session_id();
        $_SESSION['user_id'] = $user_id;
        header("Location: ../dashboard.php");
        exit();
    } else {
        header("Location: ../index.php?error=1");
        exit();
    }
} else {
    header("Location: ../index.php");
    exit();
}
?>