<?php

// get the posted data
$colourname = "";
$colourid = 0;

// check for colour data
if(isset($_POST['colourid']))
    $colourid = $_POST['colourid'];
if(isset($_POST['colourname']))
    $colourname = $_POST['colourname'];

// check and see if this is a new colour with no description - if so return to list
if ($colourid == 0 && empty($colourname))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "PannotinaCoDB");
if ($dbconn->connect_error > 0) {
    die('Could not connect: ' . $dbconn->connect_error);
    exit();
}

// lets determine if this is a new colour or existing
if ($colourid == 0)
{
    // new colour - create the query (prepared statement)
    $sql = $dbconn->prepare("INSERT INTO colour (colourname) values (?)");
    $sql->bind_param('ss', $colourname);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing colour option
    $sql = $dbconn->prepare("update colour
                                    set colourname = ?
                                    where colourid = ?");
    $sql->bind_param('ssi', $colourname, $colourid);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
?>


