Attempting to add page views using PDO
I am attempting to update page views ever time a webpage is loaded.
Every time the page loads it runs the following function, but it doesn't
add 1 to the post_views row in the mysql database.
function addPostView($post_id, $dbh){
$stmt = $dbh->prepare('SELECT post_views FROM crm_posts WHERE
post_id=?');
$stmt->bindValue(1, $post_id);
$stmt->execute();
while($views = $stmt->fetch(PDO::FETCH_ASSOC)) {
$addView = $views++;
}
$stmt2 = $dbh->prepare('UPDATE crm_posts SET post_views=? WHERE
post_id=?');
$stmt2->bindValue(1, $addView);
$stmt2->bindValue(2, $post_id);
$stmt2->execute();
}
I am running the function simply as follows:
if(isset($_GET['post_id']) && checkPostID($_GET['post_id'], $dbh)!= 0){
$post_id = $_GET['post_id'];
addPostView($post_id, $dbh);
...
As you can see I am attempting to use two prepared statements in the same
function to a) get the current number of post views and then b) update the
post views by adding one, but it isn't updating at all.
Thanks
No comments:
Post a Comment