我正在使用CodeIgniter构建一个网站,我遇到了一个问题:
当我加载歌曲页面的视图时,我还想在数据库中更新视图计数,但它更新为2。我不明白为什么会发生这种情况,因为当我按下下载按钮来更新下载次数时尝试相同的方法时,那个工作正常。
以下是代码:
function view($slug) {
// Get the song from database by slug
$data['song'] = $this->mdl_song->get_where_row("slug", $slug);
// if thre is a song, continue
if ($data['song']) {
// Set data to send to the view file
$data['module'] = "song";
$data['view_file'] = "single";
$data['page_title'] = "Download {$data['song']->name}";
// echo the template that display the view
echo Modules::run('template/two_col', $data);
// Update the song hits
$this->mdl_song->_update($data['song']->id, array('hits' => $data['song']->hits + 1));
} else {
show_404();
}
}
function _update($id, $data) {
$table = $this->get_table();
$this->db->set($data);
$this->db->where('id', $id);
$this->db->update($table);
}