【问题标题】:While loop in a while does loop forever PHP MYSQLWhile循环在一段时间内确实循环永远PHP MYSQL
【发布时间】:2016-02-12 14:42:24
【问题描述】:

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

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

我知道这与以下内容有关:

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++;
}
?>

请帮忙。

【问题讨论】:

  • while ($scorecounter &lt;= $scorecounter),这会给你一个无限循环,因为$scorecounter 将永远等于它自己。
  • 我需要改成什么,这样就等于填写的目标数了
  • 除了@NaijaProgrammer:$doelpuntcounter在哪里增加(外循环)???
  • 从您的问题来看,完全缺乏对规划和安全的理解和明显的无能 - 您将从阅读 PHP: The Right Way 中受益。我对您的建议是解决您的问题,计划您的解决方案,然后编写代码。显然你是从上面开始的初学者,所以你应该花一些时间研究和学习更多关于 PHP 的知识。

标签: php mysql while-loop phpmyadmin


【解决方案1】:

您的代码中有两个缺陷。我已经打算编写代码并添加了一些 cmets。请同时阅读下面的警告。

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

【讨论】:

    【解决方案2】:

    问题出在下面一行:

    while ($scorecounter &lt;= $scorecounter) { ... }

    您也可以将其视为检查 1 是否小于或等于 1。这等效于 while (true) { ... },因此将导致无限循环。您的意思可能是:

    while ($scorecounter &lt;= $totalgoals) { ... }

    【讨论】:

      【解决方案3】:

      正如评论中已经指出的那样:

      while ($scorecounter &lt;= $scorecounter)

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

      你需要这样的东西来停止无限循环:

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

      【讨论】:

        猜你喜欢
        • 2014-05-30
        • 1970-01-01
        • 2021-11-18
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        相关资源
        最近更新 更多