开发者

PHP and MySQL hit counter multiple pages count problem?

The following script will update the database, but it won't display the correct page count on each different page. It will freeze the page count on the page most of the time for some reason.

Here is the PHP code:

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

// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");开发者_如何学C
$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];
?>


It would have been better if you give table structure, because way I have understood this table structure is you are using page string as id, its evident from your insert query select id as page ... where page =... I do not know your table structure. So I cannot say problem is with insert or update.

If my above assumption is right you placed page in place of id in update query where clause, change update statement to -

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜