<?php

// get the posted data
$GameID = 0;
$description = "";

if(isset($_POST['GameID']))
    $GameID = $_POST['GameID'];
if(isset($_POST['desc']))
    $description = $_POST['desc'];

// check and see if this is a new game with no description - if so return to list or give error ???
if ($GameID == 0 && empty($description))
{
    $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 ($GameID == 0)
{
    // new game - create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO games (GameName) values (?)");
    $sql->bind_param('s', $description);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing Game
    $sql = $dbconn->prepare("update games 
                                    set GameName = ? 
                                    where GameID = ?");
    $sql->bind_param('si', $description, $GameID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


