35849-vm/add_task.php
2025-11-19 15:33:48 +00:00

32 lines
818 B
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$description = $_POST['description'] ?? '';
$user_id = $_SESSION['user_id'];
if (!empty($title)) {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO tasks (title, description, user_id) VALUES (?, ?, ?)");
$stmt->execute([$title, $description, $user_id]);
} catch (PDOException $e) {
header("Location: index.php?error=" . urlencode("Database error: " . $e->getMessage()));
exit;
}
}
header("Location: index.php?success=1");
exit;
} else {
header("Location: index.php");
exit;
}