doubi7496 2016-10-11 20:12 采纳率: 0%
浏览 43
已采纳

MySQLi到PHP而在html表中循环只在if语句中迭代一次

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.

  • 写回答

1条回答 默认 最新

  • douren1928 2016-10-11 20:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?