-3

The first query works fine and it retrieves the class id but I have a problem with the second query as it does not retrieve the child name

<select name="child-name" id="Schild">
<?php
    $username = $_SESSION['username'];
    $sql1 = "SELECT Class_id FROM Class WHERE Emp_id = '$username'";
    $result = mysqli_query($database,$sql) or die(mysqli_error($database));

    while($row = mysqli_fetch_array($result))
    {
        $classID = $row['Class_id'];
        $sql2 = "SELECT Name FROM Children WHERE Class_id = '$classID'";
        $result2 = mysqli_query($database,$sql2) or die(mysqli_error($database));
        while($row = mysqli_fetch_array($result2))
?>
    <option><?php echo $row['Name'];?></option>  

<?php 
        }
?> 
</select>
Rotimi
  • 4,783
  • 4
  • 18
  • 27

1 Answers1

1

You are using $row in both resultset fetches. Therefore destroying it for the outer loop

While testing always add these lines, specially if you are testing on a live/hosted server where error reporting will almost definitely be turned off

<?php
    ini_set('display_errors', 1); 
    ini_set('log_errors',1); 
    error_reporting(E_ALL); 
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>

<select name="child-name" id="Schild">
<?php
    $username = $_SESSION['username'];
    $sql1 = "SELECT Class_id FROM Class WHERE Emp_id = '$username'";
    $result = mysqli_query($database,$sql1) or die(mysqli_error($database));
    // Late breaking fix             ^^^^

    while($row = mysqli_fetch_array($result))
    {
        $classID = $row['Class_id'];
        $sql2 = "SELECT Name FROM Children WHERE Class_id = '$classID'";
        $result2 = mysqli_query($database,$sql2) or die(mysqli_error($database));
        while($row2 = mysqli_fetch_array($result2)) {
    // fix    ^^^^^
?>
    <option value="<?php echo $row2['Name'];?>"><?php echo $row2['Name'];?></option>  
<?php 
    // fix             ^^^^^
        }
?> 
</select>
<?php
    // also add terminator for first while loop
    }
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149