<?php

// get the posted data
	$PlayerID = 0; //0 new data
    $PlayerForename = ""; 
    $PlayerSurname = "";
    $PlayerAge = 0; 
    $PlayerEmail = "";
    $PlayerDateOfBirth = "";
	$PlayerNationality = "";
	$PlayerGender = "";
	$ProfessionalAlias = "";

if(isset($_POST['PlayerID']))
    $PlayerID = $_POST['PlayerID'];

if(isset($_POST['Forename']))
    $PlayerForename = $_POST['Forename'];

if(isset($_POST['Surname']))
    $PlayerSurname = $_POST['Surname'];

if(isset($_POST['Age']))
    $PlayerAge = $_POST['Age'];

if(isset($_POST['Email']))
    $PlayerEmail = $_POST['Email']; 

if(isset($_POST['DateOfBirth']))
    $PlayerDateOfBirth = date('Y-m-d', strtotime($_POST['DateOfBirth']));

if(isset($_POST['Nationality']))
    $PlayerNationality = $_POST['Nationality'];

if(isset($_POST['Gender']))
    $PlayerGender = $_POST['Gender'];

if(isset($_POST['Battletag']))
    $ProfessionalAlias = $_POST['Battletag'];




// check and see if this is a new player with no description - if so return to list or give error ???
if ($PlayerID == 0 && empty($PlayerForename) && empty($PlayerSurname) && empty($PlayerAge) && empty($PlayerEmail) && empty($PlayerDateOfBirth) && empty($PlayerNationality) && empty($PlayerGender)  && empty($ProfessionalAlias) )
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "overwatchprofessionals");
if ($dbconn->connect_error > 0) {
    die('Could not connect: ' . $dbconn->connect_error);
    exit(); // you should redirect to some form of error page !!!!
}

// determines if new pro player or not
if ($PlayerID == 0)
{
    // new player - creates the row
    $sql = $dbconn->prepare("INSERT INTO personalinfo (PlayerForename, PlayerSurname, PlayerAge, PlayerEmail, PlayerDateOfBirth, PlayerNationality, PlayerGender, ProfessionalAlias  ) values (?, ?, ?, ?, ?, ? ,? ,?)");
    $sql->bind_param('ssisssss', $PlayerForename, $PlayerSurname, $PlayerAge, $PlayerEmail, $PlayerDateOfBirth, $PlayerNationality, $PlayerGender, $ProfessionalAlias);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing player
    $sql = $dbconn->prepare("update personalinfo 
                                    set PlayerForename = ?, PlayerSurname = ?, PlayerAge = ?, PlayerEmail = ?, PlayerDateOfBirth = ?, PlayerNationality = ?, PlayerGender = ?, ProfessionalAlias = ?
                                    where PlayerID = ?");
    $sql->bind_param('ssisssssi', $PlayerForename, $PlayerSurname, $PlayerAge, $PlayerEmail, $PlayerDateOfBirth, $PlayerNationality, $PlayerGender, $ProfessionalAlias, $PlayerID );	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


