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