2

I have a problem with this code in that the second while loop only runs the first time through the code, and then also it makes it so that the $row['id'] does not get a value. Can you help me figure out where I went wrong with this?

$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>';
  }
2
  • 2
    use a different variable for $row in the inner loop. Commented Jun 29, 2013 at 2:14
  • that fixed the $row['id'] problem Commented Jun 29, 2013 at 2:15

3 Answers 3

4

Another issue I see with your code is that you will only be able fetch the result set of the query once. So instead get the values beforehand, and store them in a variable like so.

$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>';
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Can you help me with this question, it is using this same code just a little differently. stackoverflow.com/questions/17376508/…
2

It's because you are redeclaring $row, try:

$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>';
  }

2 Comments

that only fixes my second problem
the option tag only shows up on the first instance of the while loop
2

Rename your $row in your second while condition for $row2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.