查询会使所有行都正确,但第9行除外

时间:2014-06-16 16:12:57

标签: php mysql sql pdo

这是代码。每当我输入数据,它工作正常。对于游戏#9,它会正确地计算每一行。每一场比赛都被正确计算,但比赛#9。我坐下来问了3个人的帮助。现在我转向你,帮我弄清楚为什么会这样。

这是代码:

<?php

include ("../conn_inc.php");
include ("../conn_inc_pdo.php");
require_once ('auth.php');

$game_id= $_POST['game_id'];
$resultat= $_POST['namn'];
$lag1=substr($resultat, 0,1);
$lag2=substr($resultat, 2,1);


if ($lag1>$lag2){
    $tips="1";
}
if ($lag1<$lag2){
    $tips="2";
}
if ($lag1==$lag2){
    $tips="x";
}

//echo $tips;

    //------------------FIND ALL ALREADY POSTED GAMES---------------\\

    $sql2 = $pdo->prepare("SELECT game_id FROM vm2014res WHERE :game_id = game_id");
    $sql2->execute(array(':game_id' => $game_id));

    if ($sql2->rowCount() == 0) {

        //------ IRRELEVANT TEST CODE 3 LINES DOWN-----\\
        echo $game_id;
        echo $resultat;
        echo "<br>";


           //----------- INSERT THE VALUE OF THE GAME IF IT DOESNT EXIST -------\\
            $q = "INSERT INTO vm2014res (game_id, resultat, added)  VALUES (:game_id, :resultat, CURRENT_TIMESTAMP)";
            $query = $pdo->prepare($q);
            $query->execute(array(
                ':game_id' => $game_id,
                ':resultat' => $resultat
                ));

                     //------------------CHECK IF YOU SHOULD BE GETTING POINTS FOR A CORRECT GUESSED RESULT---------------\\
            $SQL = $pdo->prepare("SELECT * FROM vm2014 WHERE game_id = :game_id AND resultat = :resultat");
            $SQL->execute(array(':game_id' => $game_id, ':resultat' => $resultat));
            $r = $SQL->fetchAll(PDO::FETCH_ASSOC);
            foreach($r as $row){    


               $bet_id = $row['bet_id'];
               $game_id = $row['game_id'];
               $tips = $row['tips'];
               $resultat = $row['resultat'];
               $namn = $row['namn'];
               $points = $row['points'];

               //-------- ALL ECHOS DOWN HERE ARE IRRELEVANT----------\\
               echo $bet_id;
               echo $game_id;
               echo $tips;
               echo $resultat;
               echo $namn;
               echo $points;
               echo "<br>";
               $points=$points+2;
               echo $points;

    //------------- UPDATE THE POINTS GOTTEN (2 POINTS)---------\\

            $sql2 = $pdo->prepare("UPDATE vm2014 SET points = :points WHERE bet_id = :bet_id");
            $sql2->execute(array(':points' => $points,
            ':bet_id' => $bet_id
            ));

            }

            //------------------CHECK IF YOU SHOULD BE GETTING POINTS FOR RIGHT GUESSED GAME---------------\\
            $SQL = $pdo->prepare("SELECT * FROM vm2014 WHERE game_id = :game_id AND tips = :tips");
            $SQL->execute(array(':game_id' => $game_id, ':tips' => $tips));
            $r = $SQL->fetchAll(PDO::FETCH_ASSOC);
            foreach($r as $row){    


               $bet_id = $row['bet_id'];
               $game_id = $row['game_id'];
               $tips = $row['tips'];
               $resultat = $row['resultat'];
               $namn = $row['namn'];
               $points = $row['points'];

             //-------IRRELEVANT ECHOS DOWN HERE--------\\
               echo $bet_id;
               echo $game_id;
               echo $tips;
               echo $resultat;
               echo $namn;
               echo $points;
               echo "<br>";
               $points=$points+1;
               echo $points;

    //----------------- UPDATE YOUR POINTS IF YOU SHOULD BE GETTING POINTS FOR THE GAME----\\

            $sql2 = $pdo->prepare("UPDATE vm2014 SET points = :points WHERE bet_id = :bet_id");
            $sql2->execute(array(':points' => $points,
            ':bet_id' => $bet_id
            ));

            }

    } else {
        //-----------------POST WHEN THE GAME RESULT WAS ADDED TO THE DB-------\\
            $SQL6 = $pdo->prepare("SELECT * FROM vm2014res WHERE game_id = :game_id");
            $SQL6->execute(array(':game_id' => $game_id));
            $ra = $SQL6->fetchAll(PDO::FETCH_ASSOC);
            foreach($ra as $row){ 

               $game_id = $row['game_id'];
               $resultat = $row['resultat'];
               $added = $row['added'];

        echo "Resultatet är redan registrerat. Det registrerades: <strong>" . $added . "</strong> och det vart <strong>" . $resultat . "</strong> i matchen.";
            }
    }

我已经包含了整个代码部分,因为我不知道代码的错误部分在哪里。它应该在接下来的部分:

//------------------Kolla om du ska få poäng för ditt tips---------------\\

但我无法看到问题。

我输入2-1到游戏9.这是数据库的输入:(我不知道如何在这个网站上制作一个表格)

    bet_id  game_id tips    resultat    name      poäng 
    9           9   1       2-1         Player 1  2 
    57          9   x       1-1         Player 2  0 
    105         9   x       1-1         Player 3  0 
    153         9   1       2-0         Player 4  0 
    201         9   1       2-1         Player 5  2 
    249         9   1       3-0         Player 6  0 
    297         9   x       1-1         Player 7  0 
    345         9   x       0-0         Player 8  0 
    393         9   2       0-1         Player 9  1 
    441         9   1       1-0         Player 10 0 
    489         9   2       0-3         Player 11 1 
    537         9   x       0-0         Player 12 0 
    585         9   x       1-1         Player 13 0 
    633         9   1       2-0         Player 14 0 
    681         9   2       1-2         Player 15 1 
    729         9   2       0-1         Player 16 1 
    777         9   x       1-1         Player 17 0 
    825         9   1       3-0         Player 18 0 
    873         9   2       2-3         Player 19 1 
    921         9   2       0-1         Player 20 1 
    969         9   x       2-2         Player 21 0 
    1017        9   x       2-2         Player 22 0 
    1065        9   2       2-1         Player 23 3 
    1113        9   x       0-0         Player 24 0 
    1161        9   2       1-1         Player 25 1 

正如你所看到的,有些玩家因错误而得分。有些人获得了正确结果的积分,但没有猜到谁赢了比赛。

有人可以帮我弄清楚什么是错的?

1 个答案:

答案 0 :(得分:0)

首先你要$tips

if ($lag1>$lag2){
  $tips="1";
}

然后,用数据库结果覆盖它:

//------------------CHECK IF YOU SHOULD BE GETTING POINTS FOR A CORRECT GUESSED RESULT---------------\\
/* ... */
$tips = $row['tips'];

其中一个变量应该有不同的名称。

其他一些问题:

$resultat= $_POST['namn'];

我假设$resultat是游戏的结果,而'namn'是玩家的名字。这可能不正确。

$lag1=substr($resultat, 0,1);
$lag2=substr($resultat, 2,1);

如果一支球队在一场比赛中得分超过9球怎么办?您应explode '-'作为分隔符的结果。