[英]php counter increment error
每當用戶點擊圖像時我正試圖將計數器增加到加1。 我寫了下面的代碼但它說了一些錯誤“警告:mysql_fetch_array()期望參數1是資源,布爾值在第72行的C:\\ xampp \\ htdocs \\ tkboom \\ includes \\ core.php”中給出。 任何人都可以看到我犯了錯誤的地方..
實際上我創建了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" /> '.$playRt.' <b>|</b> '.$playCt.' plays </div></div>';
echo $image;
}
}
?>
這很可能意味着sql語句中存在錯誤。 您可以通過mysql_error()獲取有關錯誤的更多信息。
最簡單的形式:
$counter_res = mysql_query($counter_query) or die(mysql_error());
(編輯:...最簡單的形式,但是使用這種方法你不會給應用程序一個機會對問題作出反應,“死”就像“死”一樣。而mysql_error()可能會泄漏太多信息給用戶您的網絡服務/網站,請參閱https://www.owasp.org/index.php/Top_10_2007-Information_Leakage_and_Improper_Error_Handling )
您的代碼也很容易
這是因為您在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']
避免意外;)
如果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注入
首先轉換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
}
並檢查鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.