2013-05-17 37 views
0

這是我的表likes堅持更新列值

id type parent country votes 
1 1 0  US  0 # This value need to be 9 
2 2 1  US  6 
19 3 1  US  3 
3 3 2  US  3 
7 3 2  US  3 
4 10 3  US  1 
5 10 3  US  1 
6 10 3  US  1 
10 10 7  US  1 
9 10 7  US  1 
8 10 7  US  1 
20 10 19  US  1 
21 10 19  US  1 
22 10 19  US  1 

我做了一個腳本,在表中更新的總票數。

這裏,type 10 updates type 3type 3 updates 2 and 1type 2 updates 1

你會看到它是如何工作當u運行我的腳本。

這裏,id 1需要爲9,並且每次腳本運行時都不應刷新。其他人不。但我無法找到一種方法來更新1而不會使其價值翻倍。

你能幫我找個辦法嗎?

繼承人的腳本。

$conn = connect(); 

$what = 10; 
$pathType = 15; 

while ($pathType >=2) 
{ 
$stmt = $conn->prepare("select max(type) as type from likes where type < :type and country = 'US'"); 
$stmt->bindParam(':type', $pathType); 
$stmt->execute(); 
$pathData = $stmt->fetch(); 
$pathType = $pathData['type']; 
echo 'Path Type is '.$pathType.'<br>'; 

    $stmt = $conn->prepare("select sum(votes) as votes, parent as parent from likes where type=:type group by parent"); 
    $stmt->bindParam(':type', $pathData['type']); 
    $stmt->execute(); 
    $rows = $stmt->rowCount(); 

     while($row = $stmt->fetch()) { 
      echo $row['parent']." ".$row['votes']; 
      echo "<br>"; 

      if($row['parent'] == 1){ 
      echo 'Passed Level 1<br>'; 
       $wtf = $conn->prepare("update likes set votes=votes+:votes where id=:parent"); 
      }else{ 
       $wtf = $conn->prepare("update likes set votes=:votes where id=:parent"); 
      } 

      $wtf->bindParam(':votes', $row['votes']); 
      $wtf->bindParam(':parent', $row['parent']); 
      $wtf->execute(); 
     } 
    echo "-----------------------------------------------<br>"; 
} 

這裏,r的產生的情況下,你需要他們:

-- -------------------------------------------------------- 
-- Host:       127.0.0.1 
-- Server version:    5.5.25 - MySQL Community Server (GPL) 
-- Server OS:     Win64 
-- HeidiSQL version:    7.0.0.4053 
-- Date/time:     2013-05-17 14:41:11 
-- -------------------------------------------------------- 

/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET NAMES utf8 */; 
/*!40014 SET FOREIGN_KEY_CHECKS=0 */; 

-- Dumping database structure for wwp-db 
DROP DATABASE IF EXISTS `wwp-db`; 
CREATE DATABASE IF NOT EXISTS `wwp-db` /*!40100 DEFAULT CHARACTER SET utf8 */; 
USE `wwp-db`; 


-- Dumping structure for table wwp-db.likes 
DROP TABLE IF EXISTS `likes`; 
CREATE TABLE IF NOT EXISTS `likes` (
    `id` int(10) NOT NULL AUTO_INCREMENT, 
    `type` tinyint(1) DEFAULT '0', 
    `parent` int(10) DEFAULT '0', 
    `country` varchar(2) DEFAULT NULL, 
    `votes` int(10) DEFAULT '0', 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM AUTO_INCREMENT=4176 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; 

-- Dumping data for table wwp-db.likes: 14 rows 
/*!40000 ALTER TABLE `likes` DISABLE KEYS */; 
INSERT IGNORE INTO `likes` (`id`, `type`, `parent`, `country`, `votes`) VALUES 
    (1, 1, 0, 'US', 9), 
    (2, 2, 1, 'US', 6), 
    (3, 3, 2, 'US', 3), 
    (4, 10, 3, 'US', 1), 
    (5, 10, 3, 'US', 1), 
    (6, 10, 3, 'US', 1), 
    (7, 3, 2, 'US', 3), 
    (8, 10, 7, 'US', 1), 
    (9, 10, 7, 'US', 1), 
    (10, 10, 7, 'US', 1), 
    (19, 3, 1, 'US', 3), 
    (20, 10, 19, 'US', 1), 
    (21, 10, 19, 'US', 1), 
    (22, 10, 19, 'US', 1); 
/*!40000 ALTER TABLE `likes` ENABLE KEYS */; 
/*!40014 SET FOREIGN_KEY_CHECKS=1 */; 
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 

回答

0

第一行中喜歡不符合您的if語句的條件中的第一個實例。

if($row['parent'] == 1){ 
    echo 'Passed Level 1<br>'; //This doesn't apply to row with id 1 - but does to one with parent id 1 
    $wtf = $conn->prepare("update likes set votes=votes+:votes where id=:parent"); 
}else{ 
//Does not affect row with id 1 as it has a parent of 0 
$wtf = $conn->prepare("update likes set votes=:votes where id=:parent"); 
} 

編輯:好了,所以它的執行這樣的: 和更新行0

$wtf = $conn->prepare("update likes set votes=votes+:votes where id=:parent"); 

當它達到與父ID 1行,

您指示它添加到票不會設置爲一個值,每次刷新時自然會添加更多,如果您希望它停止在某個值上,則向查詢或代碼添加另一個條件

+0

它會更新值9. P這裏的問題是,如果你刷新頁面,它將會是15甚至更多。 – Norman

+0

任何想法如何做到這一點?我已經用完了想法。 – Norman

+0

如果我改變它,它會給我6而不是9。 – Norman