<?php

// get the posted data
$UserID = 0;
$UserName = "";
$UserPassword ="";


if(isset($_POST['UserID']))
    $UserID = $_POST['UserID'];

if(isset($_POST['UName']))
    $UserName = $_POST['UName'];

if(isset($_POST['UPassword']))
    $UserPassword = $_POST['UPassword'];



// return to list or give error ???
if ($UserID == 0 && empty($UserName) && empty($UserPassword))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "moviedatabase");
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 user or existing
if ($UserID == 0)
{
    // new user- create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO userlogin (Username, UserPassword) values (?,?)");
    $sql->bind_param('ss', $UserName, $UserPassword);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing user
    $sql = $dbconn->prepare("update userlogin 
                                    set Username = ?, UserPassword = ?
                                    where UserID = ?");
    $sql->bind_param('ssi', $UserName, $UserPassword, $UserID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


