【发布时间】:2016-10-11 20:12:05
【问题描述】:
我正在构建自己的自定义网站并且是 PHP 新手,我假设我在初学者级别做错了一些事情。我花了几个小时浏览各种视频教程,并梳理了这个网站以寻找类似的问题。 PHP手册看不懂,W3资源示例太简单了。
当我在没有“if ($nosubs != NULL)” if 语句的情况下运行它时,它按预期工作,但是当我添加 if 语句时,循环在一次迭代后停止。
这是 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'];
这里是 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">
我尝试了 mysqli_data_seek($querynewsubs,0);但它只是两次返回同一行。我做了一个 var_dump,查询肯定会返回两条记录,这是我希望从我的虚拟数据中看到的。在我让它正常工作后,我将添加一个更新查询以将读者分配给数据库中的提交。
我做错了什么,为什么做错了?
*注意:我还没有添加任何安全预防措施 - 我仍然只是在研究基本结构。在我研究了安全端之后,我会回去添加它。
【问题讨论】:
-
你在说什么
if? -
RTFM: php.net/manual/en/mysqli-stmt.data-seek.php 它需要一个 mysqli statement,而你没有。你没有准备一份声明。所以你的搜索失败了,你在第一次 fetch 调用中消耗的行就消失了。
-
您认为哪种情况会停止迭代?你能在每个 if 中回显或 var_dump 吗?
-
@u_mulder 抱歉,我已经更新了我的问题以反映它是 html 部分中的 ($nosubs != NULL) if 语句。
-
在while循环中你已经声明了变量
$unassignedsubs,但在循环中你正在使用$newsubs:-/
标签: php if-statement mysqli while-loop html-table