Codeigniter在使用activerecords检索时更新数据库中的列

Codeigniter在使用activerecords检索时更新数据库中的列

问题描述:

I'am learning to make a blog in CodeIgniter 2.x . What I want - when user clicks on the link readmore on the blog's page, it will redirect him to the page with corresponding id to show him a full article.

Now, while retrieving a $query->row(); from a database in content_model.php, i want to update a viewed column in database at the same time. So instead of showing him for example 34 views, it will show 35 views, and when someone open the same link it will shows him by one more - 36 views.

What's the best way to do it without making it vulnerable to hackers somehow ?

Thank you in advance for any help.

我正在学习在CodeIgniter 2.x中创建博客。 我想要的 - 当用户点击博客页面上的链接 readmore strong>时,它会将他重定向到具有相应 id code>的页面,以向他显示完整的文章。 p >

现在,从 content_model.php code>中的数据库中检索 $ query-> row(); code>时,我想更新一个查看 code>列。 因此,它不会显示 34 views code>,而是显示 35 views code>,当有人打开同一个链接时,它会再显示一个 - 36 views code>。 p>

最好的办法是什么才能让它以某种方式容易受黑客攻击? p>

提前感谢您的帮助。 p> div>

When you retrieving data from database using CI framework, you check this following:

    if ($result_from_get_function->num_rows()>0){
    return   $this->updateViewColoumnByOne();
    }else{
    return false;
    }
    function updateViewColoumnByOne(){
    //retrieve number of view in view column, suppose 12.
    //plus one with it; 12+1=13
   //update it.
   //again retrieve the new update number of views
   //and return.
   }

I think this could help you.

This should be very easy. When someone tries to read a post, just before you retrieve the post information, update the count views in the database. Execute a query that goes something like this

UPDATE `posts` SET `views` = `views` + 1;

After that, simply retrieve the post information just like you normally would, and then show it to the user by outputting it to the browser.