简体   繁体   English

html表中的MySQLi至PHP While循环在if语句中仅迭代一次

[英]MySQLi to PHP While Loop in html table iterating only once inside an if statement

I'm building my own custom website and new to PHP, and I'm assuming I've done something incorrectly at the beginner level. 我正在建立自己的自定义网站和PHP的新手,并且我假设我在初学者级别上做错了什么。 I've spent several hours looking through various video tutorials and combed through this site looking for a similar problem. 我花了几个小时浏览各种视频教程,并在这个网站上进行梳理以寻找类似的问题。 I find the PHP manual difficult to understand, and the W3 resource samples are too simple. 我发现PHP手册很难理解,并且W3资源示例太简单了。

When I run this without the "if ($nosubs != NULL)" if statement it works as expected, but when I added the if statement, the loop stopped after a single iteration. 当我在没有“ if($ nosubs!= NULL)” if语句的情况下运行此程序时,它按预期工作,但是当我添加if语句时,循环在一次迭代后停止。

Here's the PHP: 这是PHP:

<?php
require "core.php";
require "connection.php";
$output = NULL;

if (empty($_SESSION['userID'])) {
    header('Location: index.php');
} else {

    if (isset($_SESSION['userID'])) {

    $userID = $_SESSION['userID'];
    $usergroup = $_SESSION['usergroup'];
    $firstname = $_SESSION['firstname'];
    $lastname = $_SESSION['lastname'];

        if ($usergroup != 3) {
            header('Location: index.php');
            die();

        } else {

        // List unassigned submissions
        $queryopensubs = mysqli_query($dbCon, "SELECT storysubID FROM storysubs WHERE storydecision IS NULL");
        $numberopensubs = mysqli_num_rows($queryopensubs);

            if ($numberopensubs == 0) {

                $nosubs = "You do not have any new submissions";
                break;

            } else {
                $nosubs = NULL;
            }

            // Assign new submissions to first readers
            if ($numberopensubs > 0) {

                $querynewsubs = mysqli_query($dbCon, "SELECT storysubID, storywordcount, 
                    storyfilename, storysubdate, firstreadassignuserID FROM storysubs WHERE 
                    firstreadassigndate IS NULL OR firstreadassigndate=0");
                $newsubs = mysqli_fetch_assoc($querynewsubs);

                $storysubID = $newsubs['storysubID'];
                $storywordcount = $newsubs['storywordcount'];
                $storyfilename = $newsubs['storyfilename'];
                $storysubdate = $newsubs['storysubdate'];
                $firstreadassignuserID = $newsubs['firstreadassignuserID'];

And here is the html (from lower in the same file): 这是html(在同一文件的下部):

    <div id="editorunassignedsubs">

        <h4 style="border:3px white inset; width:90%; margin:25px auto 0     auto; padding:15px;">
                    New Submissions To Be Assigned</h4>

        <?php if ($nosubs != NULL) {
                    echo "<h3 style='text-align:center;padding:40px;'>".$nosubs."</h3>";
                } else {                                    
                echo "<table>";
                    echo "<tr>";
                        echo "<th>Submission #</th>";
                        echo "<th>Submission File Name</th>";
                        echo "<th>Word Count</th>";
                        echo "<th>Submission Date</th>";
                        echo "<th>First Reader ID#</th>";
                        echo "<th></th>";
                    echo "</tr>";

                while ($unassignedsubs = mysqli_fetch_assoc($querynewsubs)) :
                    echo "<tr>";
                        echo "<form name='assignfirstreader' method='post' action='upqry-assignfirstreader.php'>";
                            echo "<td>".$newsubs['storysubID']."</td>";
                            echo "<td>".$newsubs['storyfilename']."</td>";
                            echo "<td>".$newsubs['storywordcount']."</td>";
                            echo "<td>".date('M j, Y - g:ia',strtotime($newsubs['storysubdate']))."</td>";
                            echo "<td><input style='width:20px; text-align:center;' 
                                    type=text name='firstreadassignuserID' value='".$newsubs['firstreadassignuserID']."'>";
                            echo "<td><input type='submit' name='assign' value='Assign' style='font-family:Times; font-size:12pt;'>";
                            echo "</form>";
                    echo "</tr>";
                endwhile;
                echo "</table>";
                } ?>

        </div id="editorunassignedsubs">

I tried a mysqli_data_seek($querynewsubs,0); 我尝试了mysqli_data_seek($ querynewsubs,0); but it just returned the same line twice. 但它只返回了同一行两次。 I did a var_dump, and the query is definitely returning two records which is what I expect to see from my dummy data. 我做了一个var_dump,查询肯定返回了两条记录,这是我希望从虚拟数据中看到的。 After I get this working properly, I'll be adding an update query to assign readers to submissions in the db. 在此工作正常之后,我将添加一个更新查询,以将读者分配给数据库中的提交。

What have I done wrong, and why was it wrong? 我做错了什么,为什么错了?

*NOTE: I have not yet added any security precautions - I'm still just working on basic structure. *注意:我尚未添加任何安全预防措施-我仍在研究基本结构。 I'll be going back and adding it in after I study about the security end. 在研究了安全性方面之后,我将回过头来添加它。

The actual error was the new variable in the while loop. 实际的错误是while循环中的新变量。 The query's fetch variable name should have stayed the same. 查询的获取变量名称应保持不变。 I had changed it to a new variable name when I was getting only the second iteration, but did not change it back when I added in the mysqli_data_seek. 当我仅获得第二次迭代时,我已将其更改为新的变量名称,但是当我添加mysqli_data_seek时并未将其更改回。

Thanks, I now understand the variable assignment theory as it relates to the mysqli_fetch_xxx functions! 谢谢,我现在了解与mysqli_fetch_xxx函数相关的变量分配理论! I didn't really get it before, I just followed along tutorials but didn't understand what I was doing. 我之前并没有真正了解它,我只是按照教程学习,但不了解自己在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM