我需要很少的活动记录帮助。我正在尝试更新存储在数据库中的页面视图。每次 post() 函数运行时,添加 +1 。这是我的代码:
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
请帮忙!
像这样更正代码,例如:
public function post($id) {
if(!isset($id) || (int) $id<=0){
header('Location: /blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
}
标记header('Location: /blog')
- 您应该在此处放置博客的真实服务器路径。如果您使用自定义重定向函数(如您的redirect()
则在调用她之前检查 是否为输出。标头重定向仅在调用之前没有其他输出发送到浏览器时才有效。
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1',FALSE);
$this->db->update('entry');
FALSE 关闭活动记录转义,默认情况下忽略 +1。