<?php

// get the posted data
$LastLoggedIn = "";
$LoginName = "";
$LastUpdated = "";
$AccessLevel = "";
$Password = "";
$UserName = "";
$ArtistID = 0;

if(isset($_POST['UserID']))
    $ArtistID = $_POST['UserID'];
if(isset($_POST['UserName']))
    $ArtistName = $_POST['UserName'];
    if(isset($_POST['Password']))
    $ArtistName = $_POST['Password'];
    if(isset($_POST['AccessLevel']))
    $ArtistName = $_POST['AccessLevel'];
    if(isset($_POST['LastUpdated']))
    $ArtistName = $_POST['LastUpdated'];
    if(isset($_POST['LoginName']))
    $ArtistName = $_POST['LoginName'];
    if(isset($_POST['LastLoggedIn']))
    $ArtistName = $_POST['LastLoggedIn'];

// check and see if this is a new category with no description - if so return to list or give error ???
if ($ArtistID == 0 && empty($UserName, $Password ,$AccessLevel, $LastUpdated, $LoginName, $LastLoggedIn))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "madisonsmusicdatabaseofficial");
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 category or existing
if ($ArtistID == 0)
{
    // new category - create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO Users (UserName, 'Password', AccessLevel, LastUpdated, LoginName, LastLoggedIn) values (?)");
    $sql->bind_param('s', $UserName);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing category
    $sql = $dbconn->prepare("update Users
                                    set UserName = ?,
                                        Password = ?,
                                        AccessLevel = ?,
                                        LastUpdated = ?,
                                        LoginName = ?,
                                        LastLoggedIn = ?
                                    where UserID = ?");
    $sql->bind_param('si', $LastLoggedIn, $LoginName, $LastUpdated, $AccessLevel, $Password, $UserName, $UserID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


