Php 当发生下载请求时,数据库表将更新两次

Php 当发生下载请求时,数据库表将更新两次,php,mysql,download,Php,Mysql,Download,我有一个项目,我的用户将能够下载文件(pdf、文档、txt、图像等)。所以我提供了一个这样的下载链接- <li><a href="home.php?search=<?php echo $search_string; ?>&search_submit=Search&page=<?php echo $page; ?>&download=<?php echo $result_materialsID; ?>">

我有一个项目,我的用户将能够下载文件(pdf、文档、txt、图像等)。所以我提供了一个这样的下载链接-

<li><a href="home.php?search=<?php echo $search_string; ?>&search_submit=Search&page=<?php echo $page; ?>&download=<?php echo $result_materialsID; ?>">
                                            Download</a></li>
if(isset($_GET['download']))
{
    $material_to_download = $_GET['download'];
    $result_update_materials = mysqli_query($connection,"Update Materials SET numDownload=(numDownload+1) where materialsID = {$_GET['download']}");
    if(!$result_update_materials)
    {
        die("Database update numDownload materials failed:".mysqli_error($connection));
    }
    $result_get_materials_l = mysqli_query($connection,"SELECT downloadLink FROM Materials where materialsID = {$_GET['download']}");
    if(!$result_get_materials_l)
    {
        die("Database get materials failed:".mysqli_error($connection));
    }
    else
    {

        while($row = mysqli_fetch_array($result_get_materials_l))
        {

            download_file($row[0]);
        }

    }

}
它位于嵌套的ifs下,我在其中检查了其他$\u GET变量。功能下载文件如下所示:

function download_file($file)
{
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);

    }
}
问题:问题是,当我单击下载链接时,文件可以正常下载,随着代码的运行,我的数据库应该将numDownload增加1。但它增加了2。numDownload跟踪该文件的下载总数。所以我认为我的代码的更新部分实际上运行了两次


我尝试了
取消设置($\u GET['download'])
,但没有效果。知道怎么回事吗?谢谢。

您是否尝试过不使用方括号……我看不出这是问题所在,但我会像numDownload=numDownload+1那样尝试。对于初学者,您可能应该使用一个准备好的语句,而不是直接将GET['download']变量放入查询中。它让你变得脆弱。正在尝试查找您的错误。:)你是不是只想退后一排??因为你为什么要做一个带循环的fetch_数组?我认为错误应该是围绕if(isset($\u GET['download')…我们可能需要更多的代码。这些代码看起来应该在numDownload+1附近用大括号尝试,但没有帮助。并且应该是一行,因为查询是在主键上完成的。