<?php

// get the posted data
$TeamID = 0;
$TeamName = "";
$GameID = 0;

if(isset($_POST['TeamID']))
    $TeamID = $_POST['TeamID'];

if(isset($_POST['tName']))
    $TeamName = $_POST['tName'];

if(isset($_POST['gameSelect']))
    $GameID = (int)$_POST['gameSelect'];



// check and see if this is a new game with no description - if so return to list or give error ???
if ($TeamID == 0 && empty($TeamName)  && $GameID == 0)
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "gcsowls");
if ($dbconn->connect_error > 0) {
    die('Could not connect: ' . $dbconn->connect_error);
    exit(); // you should redirect to some form of error page !!!!
}

// lets determine if this is a new game or existing
if ($TeamID == 0)
{
    // new game - create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO teams (TeamName, GameID) values (?,?)");
    $sql->bind_param('si', $TeamName, $GameID);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing Game
    $sql = $dbconn->prepare("update teams 
                                    set TeamName = ?, GameID = ?
                                    where TeamID = ?");
    $sql->bind_param('sii', $TeamName, $GameID, $TeamID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


