PHP ve MySQL çoklu sayfalar sorunu saymak hit counter?

1 Cevap php

Aşağıdaki komut dosyası veritabanını güncellemek, ancak her biri farklı sayfada doğru sayfa sayısını görüntüler olmaz. Nedense çoğu zaman sayfasında sayfa sayısını donacak.

Burada PHP kodu:

<?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 Cevap

Eğer tablo yapısını verirseniz bu tablo yapısını anladım yolu select id as page ... where page =... Bilmiyorum, id olarak insert sorgusu onun belirgin sayfa dize kullanarak çünkü, daha iyi olurdu tablo yapısını biliyorum. Yani sorun ekleme veya güncelleştirme ile diyemeyiz.

Benim yukarıdaki varsayım doğru ise page id güncelleştirme sorgusu nerede fıkra, değişim güncelleme deyimi yerine yerleştirilir -

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