PHP值在刷新时进入db

时间:2013-06-27 09:00:54

标签: php mysql error-handling

我试图创造一些东西,如果一个用户选择一场比赛将赢得胜利并且该匹配赢得积分自动进入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;' ";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

  1. 选择该用户名的所有匹配项(循环显示所有匹配项)
  2. 验证:检查是否已将此添加到获胜者表
  3. 如果是这样忽略它,否则如果result = final相同(这里不知道上下文)那么存储..
  4. 问题是当您选择要验证的数据时,它们是空值,您应该在查询之前声明它们......就像这里

    $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'");
    

    这个coluld是每次在没有找到任何结果时进行引用的原因所以插入....我的建议是将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);
    

    希望这有助于交配