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