<?php
include("connectServer.php");
session_start();

if (isset($_GET['name']) && ctype_alnum ($_GET['name'])){
    $req = $bdd->prepare('SELECT * FROM gUsers WHERE NAME = "' . $_GET['name'] . '"');
	$req->execute();
    $data = $req->fetch();
	if ($data) {
        $_SESSION['indexMessage'] = "<br><br><br><p class='error'> Sorry, but this username is already in use... </p><p class='error'> Please try another one </p>";
        $_SESSION['newIndexMessage'] = true;
    }
    else {
        
        
        $req = $bdd->prepare("INSERT INTO gUsers (NAME) VALUES ('" . $_GET['name'] . "')");
        $req->execute();
        
        $_SESSION['indexMessage'] = "<br><br><p class='bigText'> Account created ! </p><p class='bigText'> Welcome on GeoQuiz " . $_GET['name'] . "!</p>";
        $_SESSION['newIndexMessage'] = true;
        
        $req = $bdd->prepare('SELECT * FROM gUsers WHERE NAME = "' . $_GET['name'] . '"');
        $req->execute();
        $data = $req->fetch();

        $_SESSION['id'] = $data['id'];
        $_SESSION['name'] = $data['name'];
    }
	
    header('Location: ../GeoQuiz', true, 302);
	exit;
}
else {
	echo "FAIL";
}
?>