<?php

// get the posted data
$playerid = 0;
$description = "";

if(isset($_POST['playerid']))
    $playerid = $_POST['playerid'];
if(isset($_POST['desc']))
    $description = $_POST['desc'];

// check and see if this is a new goalkeeper with no description - if so return to list or give error ???
if ($playerid == 0 && empty($description))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "liverpool");
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 goalkeeper or existing
if ($categoryid == 0)
{
    // new goalkeeper - create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO goalkeepers (lastname) values (?)");
    $sql->bind_param('s', $description);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing goalkeeper
    $sql = $dbconn->prepare("update goalkeepers 
                                    set lastname = ? 
                                    where playerid = ?");
    $sql->bind_param('si', $description, $playerid);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


