我的php mysql计数器脚本代码有什么问题?

时间:2017-07-07 05:18:54

标签: php mysql counter

我正在编写一个Php 5和Mysql 5页面计数器脚本。当id为“visitorid”的学生访问具有id“pageid”的页面(均为int(11))时,页面计数器会尝试在“访问者”数据库中记录访问。但是计数器没有在mysql db中更新,而是visit_counter int(4)变为0.我的代码出了什么问题? visitdate是datetime。

<?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.
}
?>

有人能告诉我这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

哦!我自己得到了答案。有时只是一些小错误让我们发疯 我在udate查询中犯了一个错误而不是使用,我应该让用户使用逗号代替。 ..现在运作良好!