我正在制作一个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++;
}
?>
请帮忙。
答案 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)
答案 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')");
}