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

// Database connection variables
$servername = "localhost";
$username = "u950110925_triggers";
$password = "[o2VJeI2";
$dbname = "u950110925_triggers";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve data from request
$data = json_decode(file_get_contents('php://input'), true);
$name = $conn->real_escape_string($data['name']);
$name = mb_substr($name, 0, 10);
$score = intval($data['score']);
$score = min($score, 1000000);


// Insert query
$sql = "INSERT INTO high_score (player, score) VALUES ('$name', '$score')";

// Execute query and respond
if ($conn->query($sql) === TRUE) {
    echo json_encode(['success' => true]);
} else {
    echo json_encode(['success' => false, 'error' => $conn->error]);
}

$conn->close();
?>
