PHP function Not Working As Expected From functions.php Include File



PHP Snippet 1:

if(!empty($_POST['download'])) {
    downloadCounter($connection);
}

PHP Snippet 2:

function downloadCounter($connection) {

    if (empty($_POST['image-id'])) {
        return;
    }

    // value from hidden form element
    $imageID = $_POST['image-id'];

    try {
        $sql = "UPDATE imageposts SET downloads = downloads +1 WHERE image_id = :image_id";
        $stmt = $connection->prepare($sql);

        $stmt->execute([
                ':image_id' => $imageID
        ]);

    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}

PHP Snippet 3:

if(isset($db_image_id)) {
    downloadCounter($connection, $db_image_id);
}

PHP Snippet 4:

if(!empty($_POST['image-id'])) {
    downloadCounter($connection, $_POST['image-id']);
}