<?php

// get the posted data
$RatingID = 0; //0 means new thingy
    $RatingName= "";
    


if(isset($_POST['RateID']))
    $RatingID = $_POST['RateID'];

if(isset($_POST['RateName']))
    $RatingName = $_POST['RateName'];



// return to list or give error ???
if ($RatingID == 0 && empty($RatingName))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "moviedatabase");
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 Rating or existing
if ($RatingID == 0)
{
    // new Rating- create the query (prepared statement
    $sql = $dbconn->prepare("INSERT INTO rating (RatingName) values (?)");
    $sql->bind_param('s', $RatingName);	
    // execute the query 
    $sql->execute();    
}
else
{
   // existing Rating
    $sql = $dbconn->prepare("update rating 
                                    set RatingName = ?
                                    where MovieID = ?");
    $sql->bind_param('si', $RatingName);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


