使用1更新列以2更新它

时间:2012-11-20 19:53:34

标签: php mysql

我有点困惑。我有一个网站,其中包含一些用户配置文件。当访问者点击用户页面时,我想按日期和用户ID更新视图数量。但是,无论我做什么,视图的数量都会更新为2而不是1。我为页面请求期间执行的所有查询创建了一个查询输出。更新查询是正确的,并且在页面请求期间只执行了1次更新查询。

这是我的数据结构:

CREATE TABLE `ProfileView` (
  `Id` int(8) NOT NULL auto_increment,
  `UserId` int(8) NOT NULL,
  `Date` date NOT NULL,
  `Views` int(8) NOT NULL,
  PRIMARY KEY  (`Id`),
  KEY `UserId` (`UserId`,`Date`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

无论我做什么,“视图”列始终更新为2而不是1。

正在执行的逻辑(从控制器调用,控制器从视图中调用。修饰器基本上是一个密封的stdClass,提供严格的编码指导,因为拼写错误的属性会导致PropertyDoesntExistException):

工作流:

# user-details.php
$oControllerProfileView = new Controller_ProfileView();
$oControllerProfileView->Replace($iUserId);

---    

# Controller.ProfileView.php
public function Replace($iUserId) {
  // validation
  Model_ProfileView::Replace($iUserId, date('Y-m-d'));
}

---

# Model.ProfileView.php
static public function Replace($iUserId, $sDate) {

    $oData = MySQL::SelectOne("
        SELECT  Views
        FROM    ProfileView
        WHERE   UserId = ".$iUserId."
        AND     Date = '".$sDate."'");

    if(is_a($oData, 'Decorator')) {

        MySQL::Query("
            UPDATE  ProfileView
            SET     `Views` = ".($oData->Views + 1)."
            WHERE   UserId = ".$iUserId."
            AND     Date = '".$sDate."'");  
    } else {

        MySQL::Query("
            INSERT INTO ProfileView
            VALUES  (
                NULL,
                ".$iUserId.",
                '".$sDate."',
                1
            )");
    }
}

0 个答案:

没有答案