MySQL查询在Chrome中的页面刷新上运行两次


MySQL query running twice on page refresh in Chrome

我正在一个论坛上工作,我已经到了想要跟踪页面视图的地步。

我决定在引入线程内容(OP)的mysql查询之后添加视图更新代码。

    public function get_threads($threadid)
    {
        $sql = "SELECT t.*, u.username, up.avatar_loc from threads t
            INNER JOIN users u
            on u.id = t.userid
            INNER JOIN user_profiles up
            on u.id = up.id
            where threadid = $threadid";
        $result = $this->db->query($sql)->row_array();
        $view = $result['numviews'];
        $view ++; 
        $update = "UPDATE threads SET numviews = $view WHERE threadid = $threadid";
        $this->db->query($update);
        return $result;
    }

但是,在Chrome中,numviews值在页面刷新时会增加两次。这在Firefox中不会发生。

通常,内容将通过AJAX调用加载,当它通过这种方式时,也不会有任何问题。在AJAX调用之后,我使用popstate来更改URL,如果用户在页面上刷新,则会收到错误,或者如果他们导航到页面(使用在新窗口中打开)。

以下是AJAX和非AJAX的控制器代码:

非Ajax线程控制器:

public function threads($threadid)
{
   //...whole lot of other stuff
   $data['content']                 = $this->components_m->thread($threadid);
   $this->load->view('template/template' ,$page);
}

AJAX控制器:

    public function ajax_thread()
    {
        $threadid = $this->input->post('threadid');
        $result['content']              = $this->components_m->thread($threadid);
        echo json_encode($result);
    }

这是components_m->thread($threadid)

    public function thread($threadid)
    {
        $data['threadid']           = $threadid;
        $data['wysihtml5_toolbar']  = $this->load->view('newthread/wysihtml5_toolbar','',TRUE);
        $data['op']                 = $this->threads_m->get_threads($threadid);
        $data['replies']            = $this->threads_m->replies($threadid);
        $page['subject']            = $data['op']['subject'];
        $page['replyTo']            = $this->threads_m->replyTo($data);
        $page['replies']                 = $this->threads_m->create_replies($data);
        return $this->load->view('thread', $page, TRUE);
    }

我搜索了我的整个项目,get_threads($threadid)只存在于上面列出的两个函数中。

当我使用$this->output->enable_profiler(TRUE);时,我只看到更新功能运行一次。

我甚至试着从ajax_thread()中注释掉$result['content'] = $this->components_m->thread($threadid);,它仍然在Chrome中更新了两次。

有什么想法吗?

通过更改UPDATE sql代码修复了它:

$update = "UPDATE threads SET numviews = numviews + 1 WHERE threadid = $threadid";
$this->db->query($update);