0
我有一個PHP腳本來記錄頁面上的點擊並將計數存儲在表中。每次會話每頁記錄一次點擊。PHP腳本不再寫入數據庫
如果在更新點擊數之前訪問過頁面。
如果一個頁面已經沒有點擊,因此沒有在表中已經存在,一個條目與數從1開始。
這是工作,但是現在它打破了小製作 - 這是更新點擊對於已經在數據庫中具有條目但不爲新頁面添加條目的頁面。
任何人都可以看到我哪裏出了問題,以及如何解決它?
該代碼;
//start a PHP session
//this prevents spamming the click count by refreshing the page
session_start();
//create current page constant
$curPage = mysql_real_escape_string(htmlspecialchars($_SERVER['REQUEST_URI']));
$title = '0';
//set number of clicks variable to 0
$clicks = 0;
//do not recount if page currently loaded
if($_SESSION['page'] != $curPage) {
//set current page as session variable
$_SESSION['page'] = $curPage;
//try to connect to MySQL server
if(!$link = mysql_connect($host, $user, $pass)) {
echo "Could not connect to MySQL server. Check your login information; the MySQL server may also be offline or temporarily overloaded.";
}
//try to select database
elseif(!mysql_select_db($dbname)) {
echo "Cannot select database.";
}
else {
//get current click count for page from database;
//output error message on failure
if(!$rs = mysql_query("SELECT * FROM click_count WHERE page_url = '$curPage'")) {
echo "Could not parse click counting query.";
}
//if no record for this page found,
elseif(mysql_num_rows($rs) == 0) {
//try to create new record and set count for new page to 1;
//output error message if problem encountered
if(!$rs = mysql_query("INSERT INTO click_count (page_url, page_count) VALUES ('$curPage', 1)")) {
echo "Could not create new click counter for this page.";
}
else {
$clicks = 1;
}
}
else {
//get number of clicks for page and add 1
$row = mysql_fetch_array($rs);
$clicks = $row['page_count'] + 1;
//update click count in database;
//report error if not updated
if(!$rs = mysql_query("UPDATE click_count SET page_count = $clicks WHERE page_url = '$curPage'")) {
echo "Could not save new click count for this page.";
}
}
}
}
更新:看來這個問題可以跟我坐在我的htaccess重寫,但我不知道爲什麼。
似乎沒有顯示任何調試信息。 – Ben