PHP 链接点击计数器
我想为链接/标签编写简单的计数器。所以我有标签并随机将它们显示到网站上
$zmienna = "SELECT name, link FROM tag_content ORDER BY RAND() LIMIT 4";
$result2 = mysql_query($zmienna);
echo $result2;
while($row=mysql_fetch_array($result2)){
echo "<a href='http://www.simplelink.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
}
,现在我想计算有多少用户点击了标签。我创建了另一行名为“wys”并尝试编写 SQL 内容,
$wtf = "UPDATE tag_content SET wys=wys+1 WHERE id=2";
$result3=mysql_query($wtf);
正如你所看到的,它仅适用于标签 id = 2。现在的问题是:如何使其适用于所有标签?
例如:我有 4 个具有不同 id 的标签。如何使计数器“读取”实际点击的标签并使其在“wys”上添加“1”?
感谢您的帮助,如果您需要更多信息(如代码等),请告诉我
I want to write simple counter for links/tags. So I have tags and random displayed them to the website
$zmienna = "SELECT name, link FROM tag_content ORDER BY RAND() LIMIT 4";
$result2 = mysql_query($zmienna);
echo $result2;
while($row=mysql_fetch_array($result2)){
echo "<a href='http://www.simplelink.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
}
And now I want to count how many users clicked tags. I created another row named "wys" and tried to write SQL stuff
$wtf = "UPDATE tag_content SET wys=wys+1 WHERE id=2";
$result3=mysql_query($wtf);
As u can see it only works for tag id = 2. And now the problem: how to make it work for all tags?
For example: I have 4 tags with different id. How to make counter "read" the actual clicked tag and make it add "1" to the "wys"?
Thx for help and let me know if u need more information (like code etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
link
字段是唯一的,因此您可以简单地将其用作标识符,而不是id
。其中
$something
是页面 URL 的最后一部分(您应该解析它)。当然,在使用之前你还需要检查这个变量,因为你从客户端获取它,并且它可能包含 SQL 注入的代码。As the
link
field is unique, you can simply use it as an identifier instead ofid
.where
$something
is a last part of page URL (you should parse it). Of course you also need to check this variable before you use it, because you got it from client side and it could have code for SQL-injection.您需要一种方法来获取所有链接的 id。要么将其作为链接的查询参数提供,要么使用从单击的链接中解析出名称和 url 并进行相关的 SELECT 来获取要循环的 id。
You need a way to get the id of all links. Either provide it as a query parameter for the link, or use parse out the name and url from the clicked link and make a relevant SELECT to get the id to loop over.
使用 jquery :eq() 选择器找出确切的链接,然后发出 ajax 请求来更新计数
Use jquery :eq() selector to find out the exact link and then make an ajax request for updating the count