<?php

// get the posted data
$AlbumID = 0;
$AlbumName = "";
$AlbumRelease = "";
$GenreID = 0;
$ArtistID = 0;

if(isset($_POST['AlbumID']))
    $ArtistID = $_POST['AlbumID'];
if(isset($_POST['AlbumName']))
    $ArtistName = $_POST['AlbumName'];
if(isset($_POST['AlbumRelease']))
    $ArtistName = $_POST['AlbumRelease'];
if(isset($_POST['GenreID']))
    $ArtistName = $_POST['GenreID'];
if(isset($_POST['ArtistID']))
    $ArtistName = $_POST['ArtistID'];

// check and see if this is a new category with no description - if so return to list or give error ???
if ($AlbumID == 0 && empty($AlbumName, $AlbumRelease, $GenreID, $ArtistID))
{
    $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 ALBUMS (AlbumName, AlbumRelease, GenreID, ArtistID) values (?)");
    $sql->bind_param('s', $AlbumName, $AlbumRelease, $GenreID, $ArtistID);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing category
    $sql = $dbconn->prepare("update ALBUMS
                                    set AlbumName = ?,
                                        AlbumRelease = ?,
                                        GenreID = ?,
                                        ArtistID = ?
                                    where AlbumID = ?");
    $sql->bind_param('si', $AlbumName, $AlbumRelease, $GenreID, $ArtistID, $AlbumID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


