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