Webbserver - Love Blomberg

Show sourcecode

The following files exists in this folder. Click to view.

public_html/GYA2/api/

admin_data.php
complete_session.php
export_csv.php
save_maze.php
save_reaction.php
save_simon.php
start_session.php

save_maze.php

49 lines ASCII Unix (LF)
<?php
require __DIR__ '/../config.php';

header('Content-Type: application/json');

if (
$_SERVER['REQUEST_METHOD'] !== 'POST') {
    
http_response_code(405);
    echo 
json_encode(['error' => 'Method not allowed']);
    exit;
}

$data json_decode(file_get_contents('php://input'), true);

if (!
$data) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid JSON']);
    exit;
}

$pid      = (int)($data['participant_id'] ?? 0);
$timeMs   = (int)($data['total_time_ms'] ?? 0);
$pathJson $data['path_json'] ?? '[]';
$moves    = (int)($data['total_moves'] ?? 0);

if (
$pid <= 0) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid participant_id']);
    exit;
}

try {
    
$pdo getDB();
    
$stmt $pdo->prepare(
        
'INSERT INTO maze_results (participant_id, total_time_ms, path_json, total_moves)
         VALUES (:pid, :time, :path, :moves)'
    
);
    
$stmt->execute([
        
':pid'   => $pid,
        
':time'  => $timeMs,
        
':path'  => $pathJson,
        
':moves' => $moves,
    ]);

    echo 
json_encode(['success' => true]);
} catch (
PDOException $e) {
    
http_response_code(500);
    echo 
json_encode(['error' => 'Database error']);
}