35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
session_start();
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['success' => false, 'message' => 'You must be logged in to subscribe.']);
|
|
exit;
|
|
}
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$location_id = $data['location_id'] ?? null;
|
|
$time = $data['time'] ?? null;
|
|
|
|
if (!$location_id || !$time) {
|
|
echo json_encode(['success' => false, 'message' => 'Location and time are required.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("INSERT INTO weather_subscriptions (user_id, location_id, delivery_time) VALUES (?, ?, ?)");
|
|
$stmt->execute([$_SESSION['user_id'], $location_id, $time]);
|
|
$subscription_id = $pdo->lastInsertId();
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Subscribed successfully!', 'subscription_id' => $subscription_id]);
|
|
} catch (PDOException $e) {
|
|
if ($e->errorInfo[1] == 1062) { // Duplicate entry
|
|
echo json_encode(['success' => false, 'message' => 'You are already subscribed to this location.']);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
?>
|