Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
session_start();
header('Content-Type: application/json');
require_once 'config.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
exit();
}
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['list_id'])) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'List ID is required']);
exit();
}
$list_id = (int)$input['list_id'];
$user_id = $_SESSION['user_id'];
try {
$pdo = getDbConnection();
// Check if user owns this list
$stmt = $pdo->prepare("SELECT user_id FROM word_lists WHERE list_id = :list_id");
$stmt->execute([':list_id' => $list_id]);
$list = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$list) {
http_response_code(404);
echo json_encode(['success' => false, 'error' => 'List not found']);
exit();
}
if ($list['user_id'] != $user_id) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'You do not have permission to delete this list']);
exit();
}
// Delete list (CASCADE will delete associated words)
$stmt = $pdo->prepare("DELETE FROM word_lists WHERE list_id = :list_id");
$stmt->execute([':list_id' => $list_id]);
echo json_encode([
'success' => true,
'message' => 'Word list deleted successfully'
]);
} catch(PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
}
?>