使用php存储数据库中的点击数 [英] Store number of clicks in database using php
问题描述
我正在开发一个包含图片库的项目。现在我想将点击次数/计数值存储到我的数据库中(在MySQL中开发)。我试图以我自己的方式做,但问题是所有图像的计数/点击值作为相同的值存储在数据库中。
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.
下面给出的代码是在文件 main.php 中,它显示从数据库检索的图像。 picture.php 是从数据库检索图像的文件。我已附加代码以计算 picture.php 中的点击次数,以便在点击图片时计算。
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.
main.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>";
}
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...");
}
推荐答案
它与SQL注入无关。
It has nothing to do with SQL injection.
问题是我在循环中调用链接,因此所有的点击值都会更新。我创建了不同的 count_link.php 文件,并且只有当用户单击图像链接时才会调用。
The problem is I'm calling the link in a loop,hence all the clicks values are getting updated.So i created a different count_link.php file and it will be called only when user click the image link.
$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=count_link.php?fid=".$offer_id.">
<img src=picture.php?fid=".$offer_id."\">
</a>
</div>
</div>";
}
guys ..:)
这篇关于使用php存储数据库中的点击数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!