-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-habit.php
More file actions
37 lines (32 loc) · 1.24 KB
/
Copy pathcreate-habit.php
File metadata and controls
37 lines (32 loc) · 1.24 KB
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
<?php
require_once 'config.php';
require_login();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_SESSION['user_id'];
$name = trim($_POST['name']);
$description = trim($_POST['description'] ?? '');
$frequency = $_POST['frequency'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'] ?? null;
$category = $_POST['category'];
// Handle custom days
$custom_days_value = null;
if ($frequency === 'custom' && !empty($_POST['custom_days'])) {
$custom_days_value = implode(',', $_POST['custom_days']);
}
try {
$stmt = $pdo->prepare("INSERT INTO habits (user_id, name, description, frequency, custom_days, start_date, end_date, category) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$result = $stmt->execute([$user_id, $name, $description, $frequency, $custom_days_value, $start_date, $end_date, $category]);
if ($result) {
$_SESSION['success'] = 'Habit created successfully!';
header('Location: habits.php');
exit;
}
} catch (PDOException $e) {
$_SESSION['error'] = 'Failed to create habit: ' . $e->getMessage();
header('Location: add-habit.php');
exit;
}
}
header('Location: add-habit.php');
exit;