<?php

// get the posted data
$patientid = 0;
$name = "";

if(isset($_POST['patientid']))
    $patientid = $_POST['patientid'];
if(isset($_POST['Name']))
    $name = $_POST['Name'];

// check and see if this is a new category with no Nameription - if so return to list or give error ???
if ($patientid == 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 category or existing
if ($patientid == 0)
{
    // new category - create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO patients (name) values (?)");
    $sql->bind_param('s', $name);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing category
    $sql = $dbconn->prepare("update patients 
                                    set Name = ? 
                                    where Patientid = ?");
    $sql->bind_param('si', $name, $patientid);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


