PHP计数器增量错误


php counter increment error

当用户每次单击图像时,我试图将计数器增加到+ 1。我写了下面的代码,但它说一些错误"警告:mysql_fetch_array()期望参数1是资源,布尔值在C:'xampp'htdocs'tkboom'包括'core.php在第72行"。谁能查一下我在哪里出错了?

实际上我已经创建了2个php文件,一个用于增加计数器,一个用于显示计数器。在core.php文件中,我编写了该函数,并创建了一个名为view.php

的文件来显示计数。
core.php
    function GenerateCount($id, $playCount) {
            global $setting;
            $counter_query = "SELECT hits FROM ava_games WHERE id=".$_GET['id']."";
            $counter_res = mysql_query($counter_query);
            while($counter_row = mysql_fetch_array($counter_res)){
               $counter = $counter_row['hits'] + 1;
               $update_counter_query = "UPDATE ava_games SET hits=".$counter." WHERE id=".$_GET['id']."";
               $playCount = mysql_query($update_counter_query);
               $playCount = $row['hits'];
            }
            return $playCount;
    // Get count END
    }
view.php
<?php
$sql = mysql_query("SELECT * FROM ava_games WHERE published=1 ORDER BY id desc LIMIT 30");
while($row = mysql_fetch_array($sql)) {
    $url = GameUrl($row['id'], $row['seo_url'], $row['category_id']);
    $name = shortenStr($row['name'], $template['module_max_chars']);
    $playRt = GenerateRating($row['rating'], $row['homepage']);
    $playCt = GenerateCount($row['id'], $row['hits']);

    if ($setting['module_thumbs'] == 1) {
        $image_url = GameImageUrl($row['image'], $row['import'], $row['url']);
        $image = '<div class="homepage_game"><div class="home_game_image"><a href="'.$url.'"><img src="'.$image_url.'" width= 180 height= 135/></a></div><div class="home_game_info"><div class="home_game_head"><a href="'.$url.'">'.$name.'</a></div></div><div class="home_game_options"><img class="home_game_options_icon" src="'.$setting['site_url'].'/templates/hightek/images/joystick-icon.png" /> &nbsp;'.$playRt.' <b>|</b> '.$playCt.' plays &nbsp;</div></div>';
        echo $image;
    }

    }
?>

这很可能意味着sql语句中有错误。您可以通过mysql_error()获取有关错误的更多信息。
最简单的形式:

$counter_res = mysql_query($counter_query) or die(mysql_error());

(编辑:…这是最简单的形式,但是使用这种方法,您不会给应用程序一个对问题做出反应的机会,"死亡"就像"死亡"一样。mysql_error()会泄露太多信息给你的webservice/网站的用户,参见https://www.owasp.org/index.php/Top_10_2007-Information_Leakage_and_Improper_Error_Handling)

你的代码也容易出现

  • sql注入,因为$_GET参数被放入语句中而没有首先对其进行消毒
  • 竞争条件,因为你有一个由一个SELECT和一个UPDATE组成的复合操作,没有任何锁定机制。

这是因为您在SQL查询中得到错误。
我改一下:

$counter_query = 'SELECT hits FROM ava_games WHERE id = ' . (int)$_GET['id'];

确保始终将id与整数值进行比较。

毕竟,这个查询看起来不太好。第一点:为什么要使用两个查询来增加一个值?UPDATE ava_games SET hits=hits+1 WHERE id=".$_GET['id'].""应该一步完成。第二点:你听说过SQL注入吗?转义或转换$_GET['id']以避免意外;)

首先将值转换为int:

function GenerateCount($playCount) {
    global $setting;
        $counter_query = "SELECT hits FROM ava_games WHERE id=".$_GET['id']."";
        $counter_res = mysql_query($counter_query);
        while($counter_row = mysql_fetch_array($counter_res)){
        $counter = intval($counter_row['hits']) + 1;
        $update_counter_query = "UPDATE ava_games SET hits=".$counter." WHERE id=".$_GET['id']."";
        $playCount = mysql_query($update_counter_query);
        $playCount = $row['hits'];
    }
    return $playCount;
// Get count END
}

和check link:

转换为int

如果mysql_query返回布尔值,则查询失败。

假设id是主键,您可以使用以下函数在数据库级别进行更新,以防止竞争条件:

function GenerateCount($playCount) {
    global $setting;
    $update_counter_query = "UPDATE ava_games SET hits=hits + 1 WHERE id=".intval($_GET['id'])."";
    mysql_query($update_counter_query) or die(mysql_error());
    $counter_query = "SELECT hits FROM ava_games WHERE id=".intval($_GET['id'])." LIMIT 1";
    list($playCount) = mysql_fetch_row(mysql_query($counter_query));
    return $playCount;
// Get count END
}

还要注意$_GET变量周围的intval(),以防止SQL注入