【问题标题】:adding hits in short url script在短 url 脚本中添加命中
【发布时间】:2012-03-20 23:59:21
【问题描述】:

我有一个简短的 url 脚本,我想向它添加链接命中功能。

当有人访问从脚本生成的链接时,点击会在数据库中更新。

我制作了一个包含以下字段的 mysql 表:

id, shortened_url, url, timestamp, hits.


  `id` int(11) NOT NULL auto_increment,
  `shortened_url` varchar(10) NOT NULL,
  `url` varchar(255) NOT NULL,
  `hits` int(11) NOT NULL,
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,

感谢任何帮助。

【问题讨论】:

  • @charmaine 你所有的问题都很糟糕。在您学习基础知识之前,不要寻求高级 Web 编程方面的帮助。您甚至不能按照下面的要求添加 echo 语句!您不能简单地剪切和粘贴到复杂的编程中。当人们试图帮助他们认为可以帮助的人时,你就是在浪费他们的时间。
  • 你的问题是什么?移过 DG。你可能是一些能手 php pro 但我真的很想学习。是你糟透了。
  • @charmaine 想学习很好,但我老实告诉你,你采取了错误的方法。在您甚至不知道如何使用echo 之前尝试使用小书签来跟踪点击量是在学习如何向后编程。我个人不在乎你学的是错误的方法还是正确的方法,但你试图通过这个网站学习错误的方法是在浪费人们的时间。从一些基本教程开始,然后首先达到合理的水平。 (而且你只是在伤害自己。将来你会回头想,“哇,DG 不是在撒谎。)
  • 感谢总干事的建议。我会记住这一点:)

标签: php mysql url short hit


【解决方案1】:

好吧,我想如果您真的希望它在数据库中得到更新,而不是使用像谷歌应用这样的解决方案,您可以执行以下操作。

$(document).ready(function(){
   $("a .scriptGenerated").bind('click', function(e){
       e.preventDefault();
       var postUrl = 'some page on your site to post to';

       var shortUrl = $(this).attr('shorturl');   ///replace short url with whatever that means
       var url = $(this).attr('href');
       var time = new Date();

       var data = {"shorturl":shorturl, "url" : url, "time" : time};

       $.post({null, postUrl, data, success:function(result){window.location = url;}});
   });
});

我认为这有一些错误,但想法是它阻止具有“scriptGenerated”类的锚执行,从中提取数据(锚DOM元素),并将其发布到您的某个页面您可以从 POST 中获取数据的应用程序。如果成功,它将重定向到相应的页面。不过,也许我误解了您要做什么。

【讨论】:

  • 我认为问题不在于重定向页面上的任意链接,而是实现postURL 页面。
【解决方案2】:

在您的短 URL 重定向页面上,执行以下查询:

SELECT url from [tablename] WHERE shortened_url=[short_url]
UPDATE [table_name] SET hits=hits+1 WHERE shortened_url=[short_url]

#redirect to url retrieved from first query

如果您愿意,可以使用shortened_url 作为表的唯一标识符(因为根据定义,每个短 URL 都必须是唯一的)。在这种情况下,不需要数字 id


试试这个:

$shortened_url = mysql_real_escape_string($shortened_url);

mysql_query("UPDATE [tablename] SET hits=hits+1 WHERE
    shortened_url='$shortened_url'");

$query = mysql_query("SELECT * FROM [tablename] WHERE
    shortened_url='$shortened_url' LIMIT 1", $db);
echo mysql_errno($link) . ": " . mysql_error($link);
$row = mysql_fetch_row($query);
if (!empty($row)) { 
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: " . $row[2]);
}

【讨论】:

  • 仍然没有工作。下面的类: [$query = mysql_query("SELECT * FROM " . $db_server . ".tablename WHERE shortened_url='" . mysql_escape_string($shortened_url) . "' LIMIT 1", $db); $row = mysql_fetch_row($query); if (!empty($row)) { header("HTTP/1.1 301 Moved Permanently"); header("位置:" . $row[2] . ""); } ]
  • @charmaine,试试更新的代码。您使用的是实际的表名,对吗?
  • 错误是什么?在最后一次查询后添加echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
  • 恐怕这对我来说太多了。毕竟我是个菜鸟。感谢您的帮助。很好的学习。
  • @charmaine,不要放弃!所做的只是输出您遇到的错误。
猜你喜欢
  • 1970-01-01
  • 2015-09-19
  • 2011-07-30
  • 2014-05-01
  • 2022-11-21
  • 1970-01-01
  • 2016-06-01
  • 2014-06-27
  • 2012-11-27
相关资源
最近更新 更多