简体   繁体   中英

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. 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.

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.

Here's the 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):

    <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); 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. 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. 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.

Thanks, I now understand the variable assignment theory as it relates to the mysqli_fetch_xxx functions! I didn't really get it before, I just followed along tutorials but didn't understand what I was doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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