php - 使用php在数据库中存储点击次数

标签 php mysql click

I am working on a project which includes image gallery.Now I want to store number of clicks/counts value into my database(developed in MySQL). I tried to do it in my own way, but problem is count/clicks value for all the images are stored in the database as same value.

The code given below is in the file main.php which displays images that is retrieved from database.The picture.php is the one that retrieves images from database. I have appended the code to count the number of clicks in picture.php so that it counts whenever the image is clicked.

ma​​in.php

    $query="SELECT * FROM files";
    $result=mysql_query($query) or die(mysql_error());
    while($fetch=mysql_fetch_array($result)){
         echo "<div class=single>
               <div class=wrap>
                   <a href=picture.php?fid=".$offer_id.">
                       <img src=picture.php?fid=".$offer_id."\">
                   </a>
               </div>
               </div>";
    }

picture.php

    if(isset($_GET['fid']))
    {
         include "connect.php";
         $fid=$_GET['fid'];
         $query="SELECT * FROM offers_em WHERE o_id =$fid";
         $result=mysql_query($query) or die(mysql_error());

         $sql="UPDATE offers_em SET count+=1 WHERE o_id=".$fid;
         $sql2=mysql_query($sql);
         
         $name=mysql_result($result,0,"pic_name");
         $size=mysql_result($result,0,"size");
         $type=mysql_result($result,0,"type");
         $content=mysql_result($result,0,"content");
   
         header("Content-Disposition: attachment; filename=$name");
         header("Content-length: $size");
         header("Content-type: $type");
         echo $content;
    }
    else{
         die("No file ID given...");
    }

最佳答案

我不确定MySQL是否支持“+=”,试试 SET 计数 = 计数 + 1

编辑:无论如何,一定要考虑对注入(inject)漏洞进行分类,如您帖子下方的评论中所述。

关于php - 使用php在数据库中存储点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13358601/

相关文章:

mysql - 找不到 Coldfusion EntityLoad get 方法,但使用 EntityLoadByPK 时,get 方法可以工作

java - 使用 Java 检测鼠标点击屏幕上的任意位置

php - 缓慢的 MySQL 查询 - 将数据缓存在 PHP 数组中?

php - symfony swiftmailer : mail not sent in prod-environment

php - 从特定 id 名称开始的 DOM 中获取 div 数据

php - 非常简单的 MySQL 语法错误,我是新手

python - 如何分组并取一列的计数除以 python pandas 中数据框第二列的唯一计数?

javascript - 用于调用方法的条件运算符

jquery - 重写 JQuery .click() 函数

PHP 数组值正确但显示不正确