<?php

// get the posted data
$PriceID = 0;
$rentalprice = "";

if(isset($_POST['PriceID']))
    $PriceID = $_POST['PriceID'];
if(isset($_POST['rentalprice']))
    $rentalprice = $_POST['rentalprice'];

// checks if there is a new price with no rentalprice
if ($PriceID == 0 && empty($rentalprice))
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
 //if not returns user back to list   
}

// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "gamerental");
if ($dbconn->connect_error > 0) {
    die('Could not connect: ' . $dbconn->connect_error);
    exit(); // if it doesnt connect error page
}

// checks the price id if its new or not
if ($PriceID == 0)
{
    // inserting the new rental price in
    $sql = $dbconn->prepare("INSERT INTO price (rentalprice) values (?)");
    $sql->bind_param('s', $rentalprice);	
    // execute the query 
    $sql->execute();    
}
else
{
   // updates to the new rental price
    $sql = $dbconn->prepare("update price 
                                    set rentalprice = ? 
                                    where PriceID = ?");
    $sql->bind_param('si', $rentalprice, $PriceID);	
    // execute the query 
    $sql->execute();      
}


// close the DB and connection
$sql->close();
$dbconn->close(); 

header('Location: index.php');
 ?>


