2013-07-10 69 views
-1

我有這個職位表設置,其中有一個hits列。 hits列用於存儲查看帖子的次數。但是,問題出在更新查詢中。數據庫位於本地(開發)機器上,因此我是唯一訪問該腳本的用戶。當我查看一篇文章,它會更新,但與假數據一樣,如果當前計數有10,當我查看它跳轉到25左右的頁面....非常隨機......但增量。我想知道我哪裏出錯了。這裏是一個代碼示例:瀏覽量的計數器更新與虛假數據

Public function getPost() 
{ 
//some data query to get post data and the post id 
$this->viewsCounter($pid); 
Return post data as array. 
} 

第一種方法是好的,現在這裏是viewsCounter()方法

Public function viewsCounter($pid) 
{ 
    $this->query("update post set hits=hits+1 where id='$pid' "); 
} 

hits列設置了這樣hits int(255) NOT NULL請幫助什麼是缺少在這裏?

+1

也許你在單個請求期間多次調用'viewsCounter()'方法? –

+0

'$ this-query'?是不是'$ this-> query'? – Faridzs

+0

hits int(255)NOT NULL錯誤使它成爲int(11)和無符號的 – Sundar

回答

1

正如@ AD7six所說,櫃檯被稱爲34時間..錯誤來自別的地方。我使用絕對的URL來加載一些js和css文件。持有這個URL的變量有點壞,資源加載不好,因此額外的意見....感謝您的幫助每個人。

這導致錯誤

$file_path='http://mysite.com/resources'; 

的代碼片段然後我的網頁上我這樣做

<link rel='stylesheet' href='<? echo $files_path ;?>/css/style.css' type='text/css' /> 

這變成了相對URL,因爲FILE_PATH變量不叫。相對URL附加到我的當前網址,因此在服務器上創建額外的命中,因爲它試圖獲取資源。

再次感謝。

+0

一些更多的細節將使這個很好的答案。無論如何,很高興你找到了問題,並意識到錯誤。 – AD7six

0
//int(255) NOT NULL change it as int(11) unsigned and validate the pid 

//add validation 
<?php 

function viewsCounter($pid) 
{ 
    //validate here 
    if(!empty($pid)) 
    { 
     $this->query("update post set hits=hits+1 where id='$pid' "); 

    }else{ 
     //your error message goes 
    } 

} 
+0

http://stackoverflow.com/questions/5238062/whats-the-difference-in-int11-and-int11-unsigned並使您的數據儘可能小http://dev.mysql.com/doc/refman/ 5.1/en/data-size.html – user20232359723568423357842364

+0

是啊,我也是...整個int 11 unsigned如何影響我的查詢和預期結果 – jcobhams

+0

我同意..我正在回答「爲什麼改變爲int 11無符號」 – user20232359723568423357842364