<?php

// get the posted data
$mediaid = 0;
if(isset($_POST['mediaid']))
    $mediaid = $_POST['mediaid'];

// check and see if this is a valid media id
if ($mediaid == 0)
{
    $redirect = "location: index.php";
    header($redirect);
    exit();
    
}
//print_r($mediaid);
// attempt to connect to the DB
$dbconn = mysqli_connect("localhost", "root", "", "mediarentals2021");
if ($dbconn->connect_error > 0) {
    die('Could not connect: ' . $dbconn->connect_error);
    exit(); // you should redirect to some form of error page !!!!
}

// delete the media
$sql = $dbconn->prepare("delete from rentals where DvdID = ? ");
$sql->bind_param('i', $mediaid);	
// execute the query 
$sql->execute(); 
// and the rentals - and yes there is better ways of doing this
$sql = $dbconn->prepare("delete from media where MediaID = ? ");
$sql->bind_param('i', $mediaid);	
// execute the query 
$sql->execute(); 
// close the DB and connection
$sql->close();
$dbconn->close(); 

// redirect back to the list
header('Location: index.php');
 ?>


