2015-12-15 65 views
6

我在php中創建了一個網站來記錄我的學校的乒乓分數,目前贏得的玩家將記錄WinnerID, LoserID, PointsFor, PointsAgainst。我有兩個表格,具有以下關係。需要幫助計算點得分的勝利和損失

表:用戶

  • USER_ID(PK)
  • 用戶名
  • 的Elo

表:遊戲

  • game_id(PK)
  • WinnerID(FK )
  • LoserID(FK)
  • PointsFor
  • PointsAgainst

在php文件我的INSERT語句是:

INSERT INTO games(WinnerID,LoserID,PointsFor,PointsAgainst) VALUES('$Winner_ID','$Loser_ID','$userscore','$oppscore')" 

以下是我已經試過了,但它不顯示分數正確。

SELECT min(u.username) 'Username', COUNT(g.WinnerID) 'Wins', sum(g.PointsFor) 'Points For', sum(g.PointsAgainst) 'Points Against', u.Elo 'Ranking' 
from games g 
LEFT JOIN users u 
on g.WinnerID = u.user_id 
Group by g.WinnerID 

This is the select statement output

你可以通過上面的圖片看,對總計爲點和點對不上號。目前,它只顯示誰是贏家的統計數據。這意味着如果PlayerA贏得21-5,它將從select語句中顯示出來,但PlayerB不會顯示5-21的分數。任何幫助表示讚賞。

的頁面中輸入分數的PHP代碼:

if(isset($_POST['btn-post'])) 
{ 
    $opponent = $_POST["opponent"]; 
    //$opponent = array_key_exists('opponent', $_POST) ? $_POST['opponent'] : false; 

    $userscore = mysql_real_escape_string($_POST['userscore']); 
    $oppscore = mysql_real_escape_string($_POST['oppscore']); 

    if($userscore != $oppscore) 
    { 
     if($userscore > $oppscore) 
     { 
      $Winner_ID = $_SESSION['user']; 
      $query = mysql_query("SELECT user_id FROM users WHERE username = '".$opponent."'"); 
      $result = mysql_fetch_array($query) or die(mysql_error()); 
      $Loser_ID = $result['user_id']; 

      $query1 = mysql_query("SELECT Elo FROM users WHERE user_id=".$_SESSION['user']); 
      $result1 = mysql_fetch_array($query1) or die(mysql_error()); 
      $winnerRating = $result1['Elo']; 

      $query2 = mysql_query("SELECT Elo FROM users WHERE user_id=".$Loser_ID); 
      $result2 = mysql_fetch_array($query2) or die(mysql_error()); 
      $loserRating = $result1['Elo']; 

      $rating = new Rating($winnerRating, $loserRating, 1, 0);    
      $results = $rating->getNewRatings(); 

      if(mysql_query("UPDATE users SET Elo = " . $results['a'] . " WHERE user_id=".$_SESSION['user'])) 
      { 

      } 
      else 
      { 
       ?> 
       <script>alert('There was an error while entering winners(user) ranking...');</script> 
       <?php 
      } 
      if(mysql_query("UPDATE users SET Elo = " . $results['b'] . " WHERE user_id=".$Loser_ID)) 
      { 

      } 
      else 
      { 
       ?> 
       <script>alert('There was an error while entering losers(opp) ranking..');</script> 
       <?php 
      } 

     } 
     elseif($oppscore > $userscore) 
     {  
      $Loser_ID = $_SESSION['user']; 
      $query = mysql_query("SELECT user_id FROM users WHERE username = '".$opponent."'"); 
      $result = mysql_fetch_array($query) or die(mysql_error()); 
      $Winner_ID = $result['user_id']; 

      //get rating from user table in database 

      $query1 = mysql_query("SELECT Elo FROM users WHERE user_id=".$_SESSION['user']); 
      $result1 = mysql_fetch_array($query1) or die(mysql_error()); 
      $loserRating = $result1['Elo']; 

      $query2 = mysql_query("SELECT Elo FROM users WHERE user_id=".$Loser_ID); 
      $result2 = mysql_fetch_array($query2) or die(mysql_error()); 
      $winnerRating = $result1['Elo']; 

      $rating = new Rating($winnerRating, $loserRating, 1, 0);    
      $results = $rating->getNewRatings(); 

      $results = $rating->getNewRatings(); 
      if(mysql_query("UPDATE users SET Elo = " . $results['b'] . " WHERE user_id=".$_SESSION['user'])) 
      { 

      } 
      else 
      { 
       ?> 
       <script>alert('There was an error while entering losers(user) ranking...');</script> 
       <?php 
      } 
      if(mysql_query("UPDATE users SET Elo = " . $results['a'] . " WHERE user_id=".$Winner_ID)) 
      { 

      } 
      else 
      { 
       ?> 
       <script>alert('There was an error while entering winners(opp) ranking...');</script> 
       <?php 
      } 

     } 
     if(mysql_query("INSERT INTO games(WinnerID,LoserID,PointsFor,PointsAgainst) VALUES('$Winner_ID','$Loser_ID','$userscore','$oppscore')")) 
     { 
      ?> 
      <script>alert('Your scores were successfully entered');</script> 
      <?php 
     } 
     else 
     { 
      ?> 
      <script>alert('There was an error while entering your score...');</script> 
      <?php 
     } 
    } 
    else 
    { 
     ?> 
     <script>alert('There cannot be a tie in ping pong, please re-enter your scores...');</script> 
     <?php 
    } 

} 
?> 
+0

所以你真正的問題是如何讓失敗者出現在你的查詢中,目前只顯示贏家? – Sean

+0

你能向我們展示一下你的'SELECT'現在返回的例子,以及你想要返回的結果嗎? –

+0

@Sean正好。它只顯示WinnerID的信息,我相信,糾正我,如果我錯了。我的問題是,積分和積分不加起來。他們會是平等的。勝利和損失也是一樣。 –

回答

1

您的查詢失敗,因爲它沒有考慮到玩家丟失的行。您可以使用unions修復該問題。下面的查詢應該做你想要什麼:

SELECT username AS "Username", 
    SUM(wins) AS "Wins", 
    SUM(PF) AS "Points For", 
    SUM(PA) AS "Points Against", 
    elo AS "Ranking" 
FROM (
    (SELECT users.user_ID, 
      users.username AS username, 
      COUNT(games.WinnerID) AS wins, 
      SUM(games.PointsFor) AS PF, 
      SUM(games.PointsAgainst) AS PA, 
      users.Elo AS elo 
    FROM users, games 
    WHERE games.WinnerID = users.user_ID 
    GROUP BY users.user_ID) 
    UNION ALL 
    (SELECT users.user_ID, 
     users.username AS username, 
     0 AS wins, 
     SUM(games.PointsAgainst) AS PF, 
     SUM(games.PointsFor) AS PA, 
     users.Elo AS elo 
    FROM users, games 
    WHERE games.LoserID = users.user_ID 
    GROUP BY users.user_ID) 
) AS t 
GROUP BY username 
ORDER BY user_ID; 

注意的是,在「查詢失敗」領域PointsAgainst應計作爲玩家的「點」,反之亦然。

+0

非常感謝!我甚至想出瞭如何把虧損列放在那裏謝謝你。非常感激! –

0

INSERT INTO games(WinnerID,LoserID,PointsFor,PointsAgainst) VALUES('$Winner_ID','$Loser_ID','$userscore','$oppscore') 

查詢中包含的潛在錯誤的順序$userscore$oppscore值。 $Winner_ID$Loser_ID可能會在您的條件處理中發生變化,但$userscore$oppscore在此時不會被類似地翻轉。

此外,您的條件結構通常是不必要的冗長。看起來你應該能夠首先確定獲勝者和輸家ID並得分,然後一次完成所有處理,而不是訴諸於容易出錯的複製粘貼複製近乎相同的代碼。

1

嘗試,因爲內部連接,並在username列擺脫MIN()的:

SELECT u.username, COUNT(g.WinnerID), 
     SUM(g.PointsFor), SUM(g.PointsAgainst), u.Elo 
    FROM users u, games g 
    WHERE u.user_id = g.WinnerID 
    GROUP BY u.username, u.Elo; 

而且,之前別人需要你的任務,你應該使用mysqli代替mysql(或更好然而,PDO),你應該使用預處理語句而不是動態SQL。

+0

這仍然會得到與我上面的'SELECT'語句相同的輸出 –

+0

讓我們確保您的數據在我們離開之前完成兔子洞下面太遠了。運行'SELECT SUM(PointsFor),SUM(PointsAgainst)FROM games;'如果這樣做,請嘗試'SELECT WinnerId,SUM(PointsFor),SUM(PointsAgainst)FROM遊戲GROUP BY WinnerID;' –