<?php    
    // 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 !!!!
    }
 
    // lets see if we have the media id if not return to the list
    $mediaid = 0;
    if (isset($_POST['mediaid']))
        $mediaid = $_POST['mediaid'];
    
    if ($mediaid == 0)
    {
        $redirect = "location: http://localhost/rentals/media";
        header($redirect);
    }

    // media id is valid - process the update
    $medianame = "";
    $category = "";
    $director = "";
    $genreid = 0;
    $supplierid = 0;
    $imagename = "";
    $pricecodeid = 0;
    
    if (isset($_POST['medianame']))
        $medianame = $_POST['medianame'];
    if (isset($_POST['category']))
        $category = $_POST['category'];
    if (isset($_POST['director']))
        $director = $_POST['director'];
    if (isset($_POST['genre']))
        $genreid = $_POST['genre']; 
    if (isset($_POST['supplier']))
        $supplierid = $_POST['supplier'];
    if (isset($_POST['pricecode']))
        $pricecodeid = $_POST['pricecode'];
    if (isset($_POST['imagename']))
        $imagename = "No Image";
    
    // perform the update 
    $sql = $dbconn->prepare("UPDATE media
                                    SET MediaName = ?,
                                        Category = ?,
                                        Director = ?,
                                        GenreID = ?,
                                        SupplierID = ?,
                                        PriceCodeID = ?,
                                        ImageName = ?
                                    WHERE MediaID = ?");    

    $sql->bind_param('sssiiisi', $medianame, $category, $director, $genreid, 
                                 $supplierid, $pricecodeid, $imagename, $mediaid);	
    // execute the query 
    $sql->execute(); 
    //close connection
    mysqli_close($dbconn);

    // redirect to view
    $redirect = "location: http://localhost/rentals/media/viewmediav2.php?mediaid=".$mediaid;
    header($redirect);
?>
