<?php 
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: *");
$name = $_GET['name'];
$score = $_GET['score'];
$servername = "localhost";
$username = "hossan";
$password = 'k/oVEq4vLATIoY';
$dbname = "zhossan";
$achievement1 = $_GET['achievement1'];
$achievement2 = $_GET['achievement2'];
$achievement3 = $_GET['achievement3'];


$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
echo "Connected successfully";


$sql = "INSERT INTO Sessie (Naam, Score) VALUES (\"$name\", $score)";

$achievementName1 = "Je bent ooit eerste geweest";
$achievementName2 = "Loser";
$achievementName3 = "Touch some grass";



if ($conn->query($sql) === TRUE) {
    echo "nieuwe record gemaakt";
    $last_id = $conn->insert_id;
    Achievements($achievement1, $achievement2, $achievement3, $last_id, $conn);
} else {
    echo "error" . $sql . "<br>" . $conn->error;
}

if ($achievement1 == 1) {
    $sqlAchievement1 = "INSERT INTO Sessie_has_Achievements (Sessie_UserID, Achievements_AchievementID) VALUES ('$last_id', 1)";
}
if ($achievement2 == 1) {
    $sqlAchievement2 = "INSERT INTO Sessie_has_Achievements (Sessie_UserID, Achievements_AchievementID) VALUES ('$last_id', 2)";
    $conn->query($sqlAchievement2);
}
if ($achievement3 == 1) {
    $sqlAchievement3 = "INSERT INTO Sessie_has_Achievements (Sessie_UserID, Achievements_AchievementID) VALUES ('$last_id', 3)";
    $conn->query($sqlAchievement3);
}
if ($conn->query($sqlAchievement1) === TRUE) {
    echo "achieve aangemaakt";
}
else {
    echo "error" . $sqlAchievement1 . "<br>" . $conn->error;
}

function Achievements($achievement1, $achievement2, $achievement3, $last_id, $conn) {

}

$conn->close();
?>
