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(); }