Files
tinyTask/add_task.php

33 lines
948 B
PHP

<?php
require 'db.php';
require 'auth.php';
header('Content-Type: application/json');
require_login();
$user_id = current_user_id();
$data = json_decode(file_get_contents('php://input'), true);
$name = trim($data['name'] ?? '');
$project_id = intval($data['project_id'] ?? 0);
if ($name === '' || $project_id <= 0) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Invalid input']);
exit;
}
// Ensure project belongs to this user
$stmt = $pdo->prepare("SELECT id FROM projects WHERE id = ? AND user_id = ? LIMIT 1");
$stmt->execute([$project_id, $user_id]);
if (!$stmt->fetchColumn()) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Forbidden']);
exit;
}
$stmt = $pdo->prepare("INSERT INTO tasks (user_id, project_id, name) VALUES (?, ?, ?)");
$stmt->execute([$user_id, $project_id, $name]);
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);