获取每个帖子的视图数量计数pdo

时间:2016-08-21 06:34:34

标签: pdo

我是pdo的新人。想要回复发布的次数。每次用户查看它将自动添加+1。我创建了一个类型为int length 16的表视图。我的代码在

之下
<?php
        try {


            $stmt = $db->query('SELECT postID, postTitle, postDesc, postDate, views FROM blog_posts ORDER BY postID ASC');
            while($row = $stmt->fetch()){

                echo '<div>';
                    echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>';
                    echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
                    echo '<p>'.$row['postDesc'].'</p>'; 
                    echo $row['views']; echo "times";           
                    echo '<p><a href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';
                    $stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;                      
                echo '</div>';

               }

        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    ?>

1 个答案:

答案 0 :(得分:0)

您只需准备更新语句不执行,因此请执行prepare语句。

 $stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ; 

 $stmt->execute(array(':postID'=>$row['postID']));