<?php

// get the posted data
	$LoginID = 0; //0 new data
    $LoginName = ""; 
    $LoginPassword = "";

if(isset($_POST['LoginID']))
    $LoginID = $_POST['LoginID'];

if(isset($_POST['Username']))
    $LoginName = $_POST['Username'];

if(isset($_POST['Password']))
    $LoginPassword = $_POST['Password'];




// check and see if this is a new user with not all details entered - if so return to list or give error ???
if ($LoginID == 0 && empty($LoginName) && empty($LoginPassword) )
{
    $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 or not
if ($LoginID == 0)
{
    // new user - creates the row
    $sql = $dbconn->prepare("INSERT INTO userlogin (LoginName, LoginPassword ) values( ?, ? )");
    $sql->bind_param('ss', $LoginName, $LoginPassword);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing user
    $sql = $dbconn->prepare("update userlogin 
                                    set LoginName = ?, LoginPassword = ? 
                                    where LoginID = ?");
    $sql->bind_param('ssi', $LoginName, $LoginPassword, $LoginID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


