-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_task.php
69 lines (59 loc) · 2.21 KB
/
get_task.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Task Detail Fetcher
*
* This script fetches the details of a specific task from the database based on the provided task ID.
* It first checks if the user is logged in, then retrieves the task details from the database, and finally
* returns the task information as a JSON response.
*
*/
// Start the session to use session variables
session_start();
// Include the database connection file
require 'dbcon.php';
// Initialize the response array
$response = [];
// Check if a valid task ID is provided via GET request
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
// Validate and sanitize the task ID
$taskId = intval($_GET['id']);
// Prepare the SQL statement to prevent SQL injection
$stmt = $con->prepare("SELECT * FROM tasks WHERE id = ?");
$stmt->bind_param("i", $taskId);
// Execute the query and get the result
if ($stmt->execute()) {
$result = $stmt->get_result();
// Check if a matching task is found
if ($result->num_rows > 0) {
// Fetch task data if a matching task is found
$task = $result->fetch_assoc();
// Populate the response array with task data
$response = [
'id' => $task['id'],
'title' => $task['title'],
'description' => $task['description'],
'assigned_by' => $task['assigned_by'],
'assigned_to' => $task['assigned_to'],
'status' => $task['status'],
'completion_date' => $task['completion_date'],
'creation_date' => $task['creation_date'],
'cage_id' => $task['cage_id']
];
} else {
// Set error response if no matching task is found
$response = ['error' => 'Task not found.'];
}
// Close the statement
$stmt->close();
} else {
// Set error response if the query execution fails
$response = ['error' => 'Error executing query: ' . $stmt->error];
}
} else {
// Set error response if the task ID is invalid
$response = ['error' => 'Invalid task ID.'];
}
// Output the response as JSON
header('Content-Type: application/json');
echo json_encode($response);
?>