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