64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Redirect to login if not logged in
|
|
if (!isset($_SESSION['worker_id'])) {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|
|
|
|
$current_worker_id = $_SESSION['worker_id'];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$job_id = isset($_POST['job_id']) ? (int)$_POST['job_id'] : 0;
|
|
$redirect_url = "job-detail.php?id=" . $job_id;
|
|
|
|
if ($job_id === 0) {
|
|
header("Location: jobs.php");
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// 1. Check if the worker has already applied for this job
|
|
$stmt_check = $pdo->prepare("SELECT id FROM job_applications WHERE job_id = :job_id AND worker_id = :worker_id");
|
|
$stmt_check->bindParam(':job_id', $job_id, PDO::PARAM_INT);
|
|
$stmt_check->bindParam(':worker_id', $current_worker_id, PDO::PARAM_INT);
|
|
$stmt_check->execute();
|
|
|
|
if ($stmt_check->fetch()) {
|
|
// The worker has already applied
|
|
header("Location: " . $redirect_url . "&application_status=error");
|
|
exit();
|
|
}
|
|
|
|
// 2. Insert the new application
|
|
$stmt_insert = $pdo->prepare("INSERT INTO job_applications (job_id, worker_id, status) VALUES (:job_id, :worker_id, 'pending')");
|
|
$stmt_insert->bindParam(':job_id', $job_id, PDO::PARAM_INT);
|
|
$stmt_insert->bindParam(':worker_id', $current_worker_id, PDO::PARAM_INT);
|
|
|
|
if ($stmt_insert->execute()) {
|
|
// Success
|
|
header("Location: " . $redirect_url . "&application_status=success");
|
|
exit();
|
|
} else {
|
|
// Failure
|
|
header("Location: " . $redirect_url . "&application_status=error");
|
|
exit();
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
// In a real app, log this error
|
|
// error_log("Application error: " . $e->getMessage());
|
|
header("Location: " . $redirect_url . "&application_status=error");
|
|
exit();
|
|
}
|
|
|
|
} else {
|
|
// If accessed directly, redirect to the jobs page
|
|
header("Location: jobs.php");
|
|
exit();
|
|
}
|