Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
session_start();
include "db.php";
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
echo json_encode(["status" => "error", "message" => "Not logged in"]);
exit();
}
// Get the JSON data from the request
$json_data = file_get_contents('php://input');
$data = json_decode($json_data, true);
if (!$data || !isset($data['words']) || count($data['words']) === 0) {
echo json_encode(["status" => "error", "message" => "No words provided"]);
exit();
}
$user_id = $_SESSION['user_id'];
$list_name = isset($data['name']) && trim($data['name']) !== '' ? $data['name'] : 'Unnamed List';
$words = $data['words'];
$is_public = isset($data['public']) && $data['public'] === true ? 1 : 0;
try {
// Start transaction
$conn->beginTransaction();
// Create new list with public flag
$stmt = $conn->prepare("INSERT INTO word_lists (user_id, name, public) VALUES (?, ?, ?)");
$stmt->execute([$user_id, $list_name, $is_public]);
$list_id = $conn->lastInsertId();
// Insert words into the list
$stmt = $conn->prepare("INSERT INTO list_words (list_id, dutch, english) VALUES (?, ?, ?)");
foreach ($words as $word) {
$stmt->execute([$list_id, $word['dutch'], $word['english']]);
}
// Commit the transaction
$conn->commit();
echo json_encode([
"status" => "success",
"message" => "List saved successfully",
"list_id" => $list_id,
"is_public" => $is_public == 1
]);
} catch (PDOException $e) {
// Roll back the transaction if something failed
$conn->rollBack();
echo json_encode(["status" => "error", "message" => "Database error: " . $e->getMessage()]);
}
?>