【问题标题】:pageviews counter updating with false data使用虚假数据更新的综合浏览量计数器
【发布时间】:2013-07-10 12:27:48
【问题描述】:

我设置了这个帖子表,其中有一个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 请帮忙看看这里有什么遗漏吗?

【问题讨论】:

  • 也许您在单个请求期间不止一次调用viewsCounter() 方法?
  • $this-query ?这不是$this->query吗?
  • 命中 int(255) NOT NULL 是错误的,使它成为 int(11) 并且无符号
  • @TomaszKowalczyk 这是一个单一的请求。如果我调用它的次数比我认为在单个请求中最多调用 35 次的次数还要多...这个错误可能会增加到预期的 35 倍
  • @AD7six 我不明白你的评论。如何重新请求帖子?...我可以从我的代码中纠正吗

标签: php mysql


【解决方案1】:

正如@AD7six 所说,计数器被调用了 34 次......错误来自其他地方。我使用绝对 url 来加载一些 js 和 css 文件。保存此 URL 的变量有点损坏,资源加载不充分,因此额外的视图....感谢大家的帮助。

这是导致错误的代码sn-p

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

然后在我的页面上我做了这个

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

这变成了相对 url,因为 file_path 变量没有被调用。相对 url 被附加到我当前的 url 上,因此在服务器尝试获取资源时会产生额外的命中。

再次感谢。

【讨论】:

  • 更多细节将使这是一个很好的答案。无论如何 +1,很高兴您发现了问题并意识到了错误。
【解决方案2】:
//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
    }

}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多