PHP和MySQL点击计数器多页计数问题?

时间:2009-10-18 06:09:24

标签: php mysql

以下脚本将更新数据库,但不会在每个不同的页面上显示正确的页数。由于某种原因,它会在大多数时间冻结页面上的页数。

这是PHP代码:

<?php
$page = $_SERVER['SCRIPT_FILENAME']; 

// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id page FROM mysql_counter_logs WHERE page = '$page'");

if (mysqli_num_rows($dbc) == 0) {
    $mysqli = new mysqli("localhost", "root", "", "sitename");
    $clean_page = mysqli_real_escape_string($mysqli, $page);
    $dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$clean_page')");
} 

if ($dbc == 1) {
    $dbc = mysqli_query($mysqli,"UPDATE mysql_counter_logs SET hits = hits + 1 WHERE page = '$page'");
}

//Retrieves the current count
$count = mysqli_fetch_row(mysqli_query($mysqli,"SELECT hits FROM mysql_counter_logs"));

if (!$dbc) {
    // There was an error...do something about it here...
    print mysqli_error();
} 

//Displays the count on your site
echo $count[0];
?>

1 个答案:

答案 0 :(得分:1)

如果你给表结构会更好,因为我已经理解了这个表结构你正在使用页面字符串作为id,从你的插入查询select id as page ... where page =...可以看出我不知道你的桌子结构。所以我不能说问题是插入或更新。

如果我的上述假设是正确的,则在更新查询where子句中放置page代替id,将更新语句更改为 -

$dbc = mysqli_query($mysqli,"UPDATE mysql_counter_logs SET hits = hits + 1 WHERE id= '$page'");