51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
header("Location: manage-tasks.php?error=invalid_task_id");
|
|
exit();
|
|
}
|
|
|
|
$taskId = (int)$_GET['id'];
|
|
$userId = $_SESSION['user_id'];
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// First, verify the task belongs to the user
|
|
$stmt = $pdo->prepare('SELECT user_id FROM tasks WHERE id = :task_id');
|
|
$stmt->bindParam(':task_id', $taskId, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$task = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$task || $task['user_id'] != $userId) {
|
|
header("Location: manage-tasks.php?error=unauthorized");
|
|
exit();
|
|
}
|
|
|
|
// Delete applications for the task
|
|
$stmt = $pdo->prepare('DELETE FROM applications WHERE task_id = :task_id');
|
|
$stmt->bindParam(':task_id', $taskId, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
|
|
// Delete the task
|
|
$stmt = $pdo->prepare('DELETE FROM tasks WHERE id = :task_id');
|
|
$stmt->bindParam(':task_id', $taskId, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
|
|
header("Location: manage-tasks.php?success=task_deleted");
|
|
exit();
|
|
|
|
} catch (PDOException $e) {
|
|
// Log the error and redirect
|
|
error_log("Delete task failed: " . $e->getMessage());
|
|
header("Location: manage-tasks.php?error=db_error");
|
|
exit();
|
|
}
|