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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器