50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in
|
|
if (!isset($_SESSION['farmer_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
// Check if scheme_id is provided
|
|
if (!isset($_GET['scheme_id'])) {
|
|
header("Location: index.php");
|
|
exit();
|
|
}
|
|
|
|
$farmer_id = $_SESSION['farmer_id'];
|
|
$scheme_id = $_GET['scheme_id'];
|
|
|
|
// Check if already applied
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id FROM applications WHERE farmer_id = ? AND scheme_id = ?");
|
|
$stmt->execute([$farmer_id, $scheme_id]);
|
|
$existing_application = $stmt->fetch();
|
|
|
|
if ($existing_application) {
|
|
$_SESSION['message'] = [
|
|
'type' => 'warning',
|
|
'text' => 'You have already applied for this scheme.'
|
|
];
|
|
} else {
|
|
// Insert new application
|
|
$stmt = $pdo->prepare("INSERT INTO applications (farmer_id, scheme_id) VALUES (?, ?)");
|
|
if ($stmt->execute([$farmer_id, $scheme_id])) {
|
|
$_SESSION['message'] = [
|
|
'type' => 'success',
|
|
'text' => 'You have successfully applied for the scheme!'
|
|
];
|
|
} else {
|
|
$_SESSION['message'] = [
|
|
'type' => 'danger',
|
|
'text' => 'There was an error processing your application. Please try again.'
|
|
];
|
|
}
|
|
}
|
|
|
|
// Redirect back to the scheme page
|
|
header("Location: scheme.php?id=" . $scheme_id);
|
|
exit();
|
|
?>
|