<?php

// get the posted data
$StaffID = 0;
$Name = "";

if(isset($_POST['StaffID']))
    $StaffID = $_POST['StaffID'];
if(isset($_POST['Name']))
    $name = $_POST['Name'];


if ($StaffID == 0 && empty($Name))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "hospital");
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 staff member or existing
if ($StaffID == 0)
{
    // new staff member - create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO staff (name) values (?)");
    $sql->bind_param('s', $Name);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing staff member
    $sql = $dbconn->prepare("update staff 
                                    set Name = ? 
                                    where StaffID = ?");
    $sql->bind_param('si', $Name, $StaffID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


