Number in database is not updated using RedBeanPHP

49 views Asked by At

Using RedBaeanPHP, I want the page to be updated and add + 1 to the "view". Please help me, I don't understand it very well.

$post_id = $_GET['post_id'];
$posts = R::load('posts', $post_id);

if (isset($_SESSION['recent_posts'])) {
    R::exec('UPDATE posts SET view = "view" + 1 WHERE id = $posts["id"]');
};

When a page is refreshed, it is not added to the database.

2

There are 2 answers

1
Taras On BEST ANSWER

Thanks, already solved the problem

$id = $posts['id'];
$view = "UPDATE `posts` SET `view` = `view` + 1 WHERE `id`= $id";
R::exec($view);
0
schmauch On

Since you have already loaded the rquested post from the database (by doing $posts = R::load('posts', $post_id);) you could use the store method instead of R::exec.... So just use $posts->view++; and R::store($posts);