<?php
# This file fetches film summaries from the database using ajax calls.
if(isset($_POST['id'])){
    $id = $_POST['id'];
    require 'db_connection.php';
    # echo "Great moves Cristiano! Keep it up.";
    
    # 2. Perform database query.
    # Query construction.
    $query =  "SELECT title, summary FROM " . TB_NAME;
    $query .= " WHERE id = ". $id;
    $query .= " LIMIT 1";
    # Contact database.
    $raw_result = mysqli_query($connection, $query);
    # Test if there was a query error.
    if($raw_result) {
        while ($film = mysqli_fetch_assoc($raw_result)) {
            # When successful, it echoes the result and script.js will handle the data.
            $summary = trim($film['summary']);
            echo $summary;
        } // Fetching results.
        /* After contacting the database results are freed to save memory. */
        mysqli_free_result($raw_result);
        /* Database connection is closed. */
        mysqli_close($connection);
    } else {
        die('Database query failed.');
    } // if $raw_result
} // if $_POST
?>