2017-07-07 60 views
-4

我正在寫一個PHP 5和Mysql 5頁面計數器腳本。當id爲'visitorid'的學生訪問id爲'pageid'的頁面(int(11))時,頁面計數器會嘗試將訪問記錄在'訪問者'數據庫中。但計數器不是在MySQL數據庫中更新,而是visit_counter int(4)變爲0.我的代碼出現了什麼問題? visitdate是日期時間。我的php mysql計數器腳本代碼有什麼問題?

<?php 
$pageid = 101; 
$visitorid = 234; 

    $sql = "SELECT * FROM visitors 
         WHERE pageid = ".$pageid." 
         AND visitorid = ".$visitorid; 

$temp = mysql_query($sql) or die("Error 1.<br>".mysql_error()); 
$data = mysql_fetch_array($temp); 

// visit_counter is a field in table 
if(($data['visit_counter']) != NULL){ 
echo "Entery exists <br>"; 
// Tried below version also 
$visit = " SET visit_counter = visit_counter+1"; 

//$visit_counter = $data['visit_counter']; 
//$visit = " SET visit_counter = ".$visit_counter++ ; 

// Valid SQL 
// UPDATE `visitors` 
//  SET visit_counter = visit_counter+1 
//  WHERE pageid = 101 and visitorid=234 
// This manual sql query updates in phpmyadmin 

$sql = "UPDATE visitors ".$visit." 
      AND visitdate = NOW() 
      WHERE pageid = ".$pageid." 
      AND visitorid = ".$visitorid; 

$temp = mysql_query($sql) or die("ERROR 3.<br>".mysql_error()); 
//No error is displayed on above query. 

} else { 
//first entry 
$visit_count = "1"; 
$sql = "INSERT INTO visitors 
      (`pageid`,`visitorid`, `visitdate`, `visit_counter`)  
    VALUES ('".$pageid."','".$visitorid."', NOW(), '".$visit_count."')"; 
$temp = mysql_query($sql); 
//first entry is inserted successfully 
//and visit_counter shows 1 as entry. 
} 
?> 

誰能告訴我這段代碼有什麼問題嗎?

回答

0

哦!我自己得到了答案。有時候只是一點點的錯誤就會讓我們變得瘋狂...... 我在udate查詢中犯了一個錯誤..而不是使用,我應該讓用戶使用逗號。 ..現在運作良好!