虽然循环一段时间确实循环PHP MYSQL

时间:2015-11-11 13:36:27

标签: php mysql while-loop phpmyadmin

我正在制作一个while循环,以便代码读出有多少目标得分。 这适用于计数器,因为您可以看到$scorecounter = 1;$scorecounter++;。 它还会增加1个进球,一个进球。 当得分为6次时,while循环次数为6次。

一切正常,但唯一不起作用的是te页面不断加载。就像它永远在做循环一样。页面没有刷新,它会一直循环第二次循环。

我知道这与某些事情有关:

while ($scorecounter <= $scorecounter) {
                            mysql_query("INSERT INTO goals (PlayerID, GameID, Name, Team, added_on) VALUES ('$IDdpt', '$Gamedpt', '$Namedpt', '$comp', '$date')");
                }

但我无法找到它永远在线的错误。

以下是我的全部代码:

<?php
$Gamedpt = $row['ID'];
$IDdpt = $_POST['ID1'];
$Namedpt = $_POST['player1']; 

$scorecounter = 1;

if (stristr($row['Hometeam'],"$club")) {
    $totalgoals = $row['Thuisscore'];
}
if (stristr($row['Awayteam'],"$club")) {
    $totalgoals = $row['Uitscore'];
}
while ($doelpuntcounter <= $totalgoals) {

if ($_POST["score1"] == "$scorecounter") {
    echo $_POST["player1"];
    echo " has $scorecounter times scored";
    mysql_query("UPDATE players SET Goals = Goals+'$scorecounter' WHERE Naam ='".$_POST["player1"]."'");
                    while ($scorecounter <= $scorecounter) {
                        mysql_query("INSERT INTO goals (PlayerID, GameID, Name, Team, added_on) VALUES ('$IDdpt', '$Gamedpt', '$Namedpt', '$comp', '$date')");
            }

    }
    $scorecounter++;
}
?>

请帮忙。

3 个答案:

答案 0 :(得分:0)

您的代码中存在两个缺陷。我打算使用代码并添加一些注释。请阅读以下警告。

while ($doelpuntcounter <= $totalgoals) {
    if ($_POST["score1"] == "$scorecounter") {
        echo $_POST["player1"] . " has $scorecounter times scored.";
        mysql_query("UPDATE players SET Goals = Goals+'$scorecounter' WHERE Naam ='".$_POST["player1"]."'");
        while ($scorecounter <= $scorecounter) {
            mysql_query("INSERT INTO goals (PlayerID, GameID, Name, Team, added_on) VALUES ('$IDdpt', '$Gamedpt', '$Namedpt', '$comp', '$date')");
            $scorecounter++; // needs to be in this while loop, flaw #1 !
        }

    }
    // additionally, where is your $doelpuntcounter incremented???
    // flaw #2
}

此外,您仍在使用旧的mysql功能。相反,请尝试使用较新的mysqli函数或PDO代替。

答案 1 :(得分:0)

问题出在以下几行: while($ scorecounter&lt; = $ scorecounter){...} 您还可以将其视为检查1是小于还是等于1.这相当于while(true){...}因此将导致无限循环。你可能意味着以下内容: while($ scorecounter&lt; = $ totalgoals){...}

答案 2 :(得分:0)

正如评论中所述:

while ($scorecounter <= $scorecounter)

会给你一个无限循环,因为$ scorecounter总是等于它自己。

你需要这样的东西来阻止infinte循环:

$loop_breaker = 0;
while ( ++$loop_breaker <= $scorecounter) {
                        mysql_query("INSERT INTO goals (PlayerID, GameID, Name, Team, added_on) VALUES ('$IDdpt', '$Gamedpt', '$Namedpt', '$comp', '$date')");
}