计数器无法注册计数数
Counter fails to register number of count
当有人点击我网页上的按钮时,这个JavaScript代码会运行:
function count() {
var xmlHttp = getXMLHttp(); // XMLHttpRequest object
xmlHttp.open("GET", "count.php", true);
xmlHttp.send(null);
}
然后,这个PHP脚本被执行:(一个常见的)
<?php
mysql_connect("host","username","password");
mysql_select_db('database');
mysql_query('UPDATE table SET field = field + 1');
?>
问题可能会有所不同。假设我点击十次,它只注册 8 或其他东西。有时它有效,有时它不起作用。
您希望禁用用于单击的按钮,直到 Ajax 请求完成并返回成功消息。例如,在StackOverflow上检查注释机制。您不能按两次,因为在第一次之后它会被禁用。
try {
$pdo = new PDO("mysql:host=localhost;dbname=tests", "user", "pass");
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//Have PDO throw exceptions on errors. That way you don't miss 'em.
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE `table` SET `field` = `field`+1";
$stmt = $pdo->prepare($sql);
$stmt->execute();
}
catch (PDOException $e) {
//In case an error has occurred, it will be caught and handled here.
die("There was an error with the database! " . $e->getMessage());
}
这就是我在PDO中的做法。
总有一个解决方案...
快速修复解决方案 @
使 AJAX 调用同步。注意:这将使"生成"按钮在执行期间似乎卡住了,我认为您不希望这样(因为您将所有名称保留在JS数组中以加快加载速度...是的,我访问了您的链接...感谢您发布)
function count() {
var xmlHttp = getXMLHttp();
var async = false;
xmlHttp.open("GET", "count.php?" + Math.random, async);
xmlHttp.send();
}
更好的解决方案
//call submit count every 60 secs, play with it to get a interval that suits you best
window.setTimeout("submitCount();",60000);
var globalCounter = 0;
function count()
{
globalCounter++;
}
function submitCount(){
if (globalCounter > 0)
{
var xmlHttp = getXMLHttp();
var async = false;
var countForPHP = globalCounter;
globalCounter = 0;
xmlHttp.open("GET", "count.php?r=" + Math.random + "&count=" + countForPHP, async);
xmlHttp.send();
}
window.setTimeout("submitCount();",60000);
}
更改您的 PHP 以从 Request.QueryString 中获取 countForPHP,并相应地更新 SQL
注意:当网站关闭并且 60 秒手未提交到服务器时,计数将丢失。要解决这个问题,您可能需要在window.onbeforeunload上调用submitCount on window.onbeforeunload
问题似乎是浏览器从缓存加载结果。 添加代码以发送标头,以便在响应计数时禁用页面缓存.php(来自 php.net 标头示例):
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
在数据库中存储单个单击(带有日期时间),而不是更新字段。为此,需要更改您的计数.php以在表中插入记录(一键)。
要获得总点击次数,您可以使用表格的"count(1)"。由于您正在插入点击的日期和时间,因此您可以从表中获取日/周/月/年明智的数据点击。
顺便说一句,更新将在更新记录/行时锁定记录/行。当多个请求尝试同时更新同一记录时(在并发请求的情况下),更新可能会因锁定问题而失败。
您的表(点击 - 表名称)可以如下所示。
+-------------+---------------------+
| sourceip | click_date |
+-------------+---------------------+
| 10.32.12.45 | 2012-08-16 13:35:03 |
+-------------+---------------------+
计数中的查询.php应该是这样的插入到点击(源IP,click_date)值('10.32.12.45',现在());
您可以使用 php 脚本获取源 ip(客户端 ip)。
下面的查询为您提供了每天/每月/每年的点击次数。天 => 选择 date_format(click_date, '%Y-%m-%d') 作为日期,按 date_format(click_date, '%Y-%m-%d'
分组的点击次数(1) 次);月 => 选择 date_format(click_date, '%Y-%m') 作为日期,按 date_format(click_date, '%Y-%m'
分组的点击次数(1) 次);年份 => 选择 date_format(click_date, '%Y') 作为日期,按 date_format(click_date, '%Y'
) 分组的点击次数(1);
试试这个:
$con = mysql_connect("host","username","password");
mysql_select_db('database', $con);
$result = mysql_query("select * from table");
$numrows = mysql_num_rows($result);
if ($numrows!=0){
while ($row = mysql_fetch_assoc ($result)){
$Field = $row['field'];
}
$FieldTwo = $Field+1;
$result = mysql_query('UPDATE table SET field='$FieldTwo');
}
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- knockoutjs可观察数组
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将数组从PHP传递到Javascript
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 在函数中添加数组元素的数值
- 计数器无法注册计数数
- 注册客户端脚本不适用于设置数组值
- 在使用 Passport 注册后将表示用户的对象添加到数组中
- 如何在jQuery-Javascript中填充一个月数数组
- 如何循环元素&数数
- 使用数组动态注册事件
- 将二进制数数组转换为相应的整数
- 数组长度属性中未注册的成员
- JavaScript数独ID字段未注册