2

我正在尝试创建一些东西,如果用户选择了一场比赛将会获胜并且该比赛获胜,那么积分会自动进入 DB .. 我做得很好,直到积分只进入 DB 一次。我的意思是正确验证,但每次刷新点进入 DB 。请帮忙。

//Ndeshjet e fituara ose jo
echo "<h3>Ndeshjet e vendosura nga <b>$username</b> dhe Rezultatet:</h3><br/>";

$query = $db-> query("SELECT * FROM match_select WHERE user_id='$username'");

while ($row = $query->fetch(PDO::FETCH_ASSOC)){                         
    $match = $db->query("SELECT * FROM `winner` WHERE `user_id` = '$username' AND  `match_id` = '$id' AND `liga`='$liga'");

    $id = $row['match_id'];
    $liga = $row['liga'];
    $koeficent = $row['selected_koef'];     

    if($match->rowCount($match)){       
        echo "";            
    }else{
        if ($row['result'] == $row['final']){
            $hey =  "style='color: green;' ";
            $match = $db -> query("INSERT INTO winner (user_id, match_id, koef, final, liga) VALUES ('$username','$id', '$koeficent', '1', '$liga')");
        }else if ($row['final']== ""){
            $hey =  "style='color: black;' ";
        }else{
            $hey =  "style='color: red;' ";
        }
    }
}
4

1 回答 1

0

关于提供的代码,据我所知;

  1. 选择该用户名的所有匹配项(遍历所有匹配项)
  2. 验证:检查是否已将其添加到获胜者表中
  3. 如果是这样忽略它,否则如果结果 = final 相同(不知道这里的上下文)然后存储..

问题是当您选择数据进行验证时,它们是空值,您应该在查询之前声明它们......就像这里

$match = $db->query("SELECT * FROM `winner` WHERE `user_id` = '$username' AND  `match_id` = '$id' AND `liga`='$liga'");

$id = $row['match_id'];
$liga = $row['liga'];
$koeficent = $row['selected_koef'];   

它应该是

 $id = $row['match_id'];
 $liga = $row['liga'];
 $koeficent = $row['selected_koef'];       
 $match = $db->query("SELECT * FROM `winner` WHERE `user_id` = '$username' AND  `match_id` = '$id' AND `liga`='$liga'");

这可能是每次引用时找不到任何结果的原因,因此插入....我的建议是将 SQL 放在不同的变量中,出于测试目的,您可以将其转储到屏幕上,例如

$sql = "SELECT * FROM `winner` WHERE `user_id` = '$username' AND  `match_id` = '$id' AND `liga`='$liga'";
echo $sql; //to see whats going on
$match = $db->query($sql);

希望这有助于交配

于 2013-06-27T09:20:38.523 回答