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