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

I have a function in a functions.php file which updates a download count in a MySQL database via php. I cannot fathom out why it isn't working.

Below is the code that invokes the function. I'm also not getting any errors showing in my error logs.

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

Below is the function this is linked to in functions.php. Other functions from this file work so I know it is being included correctly. Note: the $connection variable is the connection from a db.php file

function downloadCounter($connection, $imageID) {
    if (isset($_POST['download'])) {

        // 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();
        }
    }
}

When I do var_dump($db_image_id) when there are three images on the page I get the following when the page loads - it only references one of the 3 images by id (namely id of 164):

`string(3) "164"`

The PHP for the page is below.

<?php

include "functions.php";

if (isset($_GET['username'])) {
    $username = $_GET['username'] 
} else { 
    header("Location: login.php");
    exit;
}
?>

<aside class="masonry-grid">
    <?php

        $stmt = $connection->prepare("SELECT * FROM imageposts WHERE username = :username");
        $stmt->bindParam(':username', $username);
        $stmt->execute();

        while ($image_row = $stmt->fetch()) {

        $db_image_id = escape($image_row['image_id']);
        $db_image_title = escape($image_row['image_title']);
        $db_image_tags = escape($image_row['image_tags']);
        $db_image_filename = escape($image_row['filename']);
        $db_ext = escape($image_row['file_extension']);

    ?>
    <figure>
        <form method="post">

        <!-- HTML OUTPUT OF IMAGES THAT INCLUDES ABOVE PHP VARIABLES -->

        <!-- DOWNLOAD BUTTON THAT TRIGGERS THE DOWNLOAD COUNTER FUNCTION  -->
            <button type="submit" name="download">Download</button>
            <input type="hidden" name="image-id" value="<?php echo $db_image_id; ?>">
        </form>
    </figure>
    
<?php } ?>
            
<?php

    // theoretically updates a download count in database via the functions.php function but isn't working
    if(isset($db_image_id)) {
        downloadCounter($connection, $db_image_id);
    }
?>
</aside>
php


Solution 1:[1]

Maybe you should call the function like this:

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

And your downloadCounter function could look like this:

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();
    }
}

Solution 2:[2]

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

There may be something wrong. The var $db_image_id is the last value of image_row. We could use $_POST['image-id']. For example:

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

btw, the function downloadCounter() could be improved too. You can see another answer of Onki Hara.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Onki Hara
Solution 2