PHP:更新ID为“x”的图像的数据库“num_views”

时间:2012-11-13 21:06:27

标签: php mysql

我想在点击“Paging.php”中的某个图像时增加页面浏览量(“num_views”)数据库值,这样我就可以跟踪该图像的查看次数

Paging.php:

while ($imageCounter < $imagesPerPage && ($row = $catResult->fetch_assoc())) {
    echo    "<br />ID: " . $row['imgid'] . 
            '<br /><a href="./templates/viewcomic.php?views='. $row['num_views'].'&id=' . $row['imgid'] . '&image=' . $imgpath.$row['imgname'] . '"><img src="' . $thumbpath.$row['imgthumb'] . '"/></a>' . 
            "<br />CATFK: " . $row['catfk'] . 
            "<br/>";

    $imageCounter++;
}

ViewComic.php

<?php
include 'include/header.php';

$imgid = $_GET['id']; 
$views = $_GET['views'];

include '../scripts/dbconnect.php'; 
$mysqli->query("UPDATE child_images SET num_views = ($views+1) WHERE imgid = $imgid");
mysqli_close($mysqli);
?>

虽然

似乎没有增加

2 个答案:

答案 0 :(得分:5)

更简单的方法是只增加已发布到数据库中的值。这样您就不必担心查询字符串中的数据操作。

$imgid= $mysqli->real_escape_string($imgid);    
$mysqli->query("UPDATE child_images SET num_views = num_views + 1 WHERE imgid = $imgid");
mysqli_close($mysqli);

答案 1 :(得分:2)

这样做:

$mysqli->query("UPDATE child_images SET num_views = (num_views+1) WHERE imgid = $imgid");