<?php

// get the posted data
	$HeroID = 0;
    $HeroName = ""; 
    $HeroRole = "";
    $HeroHealth = 0; //0 new data
    $HeroNationality = "";
    $HeroAge = 0;

if(isset($_POST['HeroID']))
    $HeroID = $_POST['HeroID'];

if(isset($_POST['hero']))
    $HeroName = $_POST['hero'];

if(isset($_POST['Role']))
    $HeroRole = $_POST['Role'];

if(isset($_POST['health']))
    $HeroHealth = $_POST['health'];

if(isset($_POST['Nationality']))
    $HeroNationality = $_POST['Nationality']; 

if(isset($_POST['Age']))
    $HeroAge = $_POST['Age'];




// check and see if this is a new player with no description - if so return to list or give error ???
if ($HeroID == 0 && empty($HeroName) && empty($HeroRole) && empty($HeroHealth) && empty($HeroNationality) && empty($HeroAge) )
{
    $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 ($HeroID == 0)
{
    // new player - creates the row
    $sql = $dbconn->prepare("INSERT INTO overwatchheroinfo (HeroName, HeroRole, HeroHealth, HeroNationality, HeroAge ) values (?, ?, ?, ?, ?)");
    $sql->bind_param('ssisi', $HeroName, $HeroRole, $HeroHealth, $HeroNationality, $HeroAge);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing player
    $sql = $dbconn->prepare("update overwatchheroinfo 
                                    set HeroName = ?, HeroRole = ?, HeroHealth = ?, HeroNationality = ?, HeroAge = ? 
                                    where HeroID = ?");
    $sql->bind_param('ssisii', $HeroName, $HeroRole, $HeroHealth, $HeroNationality, $HeroAge, $HeroID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


