2

我对这段代码有一个问题,因为第二个 while 循环只在第一次通过代码时运行,然后它也使得 $row['id'] 没有得到一个值。你能帮我弄清楚我哪里出错了吗?

$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
$result2 = mysqli_query($con,"SELECT * FROM sections ORDER BY `order`");

while($row = mysqli_fetch_array($result))
  {
  echo  '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
  echo  '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
  echo  '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
  echo  '<select name="section">';
  $section = $row['section'];
  while($row = mysqli_fetch_array($result2)) {
      $sectionname = $row['sectionname'];
      if ($sectionname == $section) {
        echo    '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
      } else {
            echo    '<option value="' . $sectionname . '">' . $sectionname . '</option>';
      }
  }
  echo      '</select>';
  echo      '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
  echo      '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
  echo      '<input type="submit" name="submission" value="Update">';
  echo      '<input type="submit" name="submission" value="Delete">';
  echo  '</form>';
  }
4

3 回答 3

4

我在您的代码中看到的另一个问题是您只能获取一次查询的结果集。因此,请事先获取值,并将它们存储在像这样的变量中。

$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
$sectionnames = array();
while($row = mysqli_fetch_array($result2)) {
    $sectionnames[] = $row['sectionname'];
}


$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
while($row = mysqli_fetch_array($result))
  {
  echo  '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
  echo  '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
  echo  '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
  echo  '<select name="section">';
  $section = $row['section'];
  foreach ($sectionnames as $sectionname) {
    if ($sectionname == $section) {
        echo    '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
    } else {
        echo    '<option value="' . $sectionname . '">' . $sectionname . '</option>';
    }
  }
  echo      '</select>';
  echo      '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
  echo      '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
  echo      '<input type="submit" name="submission" value="Update">';
  echo      '<input type="submit" name="submission" value="Delete">';
  echo  '</form>';
  }
于 2013-06-29T02:23:27.737 回答
2

这是因为您要重新声明$row,请尝试:

$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
$result2 = mysqli_query($con,"SELECT * FROM sections ORDER BY `order`");

while($row = mysqli_fetch_array($result))
  {
  echo  '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
  echo  '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
  echo  '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
  echo  '<select name="section">';
  $section = $row['section'];
  while($row2 = mysqli_fetch_array($result2)) {
      $sectionname = $row2['sectionname'];
      if ($sectionname == $section) {
        echo    '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
      } else {
            echo    '<option value="' . $sectionname . '">' . $sectionname . '</option>';
      }
  }
  echo      '</select>';
  echo      '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
  echo      '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
  echo      '<input type="submit" name="submission" value="Update">';
  echo      '<input type="submit" name="submission" value="Delete">';
  echo  '</form>';
  }
于 2013-06-29T02:15:17.323 回答
2

$row在第二个 while 条件下重命名您的$row2

于 2013-06-29T02:16:14.870 回答