1

I am trying to to let users choose the state filed from a drop down in the form, but although the query works, the DB is connected, the drop down does not display the list.

Not sure if relevant but i'm running Windows Server, IIS 10 and the latest PHP and MySql.

<div class="form-group col-md-3">
    <label for="state">State</label>
    <select class="form-control" name="state" id="state">
                                            
    <?php  
    // Query to find STATE
    $sql1 = "SELECT * FROM codetable WHERE category = 'state'";
    $result1 = $conn->query($sql1);

    $row1 = mysqli_fetch_array($result1);

    while($row1 = mysqli_fetch_array($result1))  
        {  
            echo '<option value=" '.$row1['code'].' "> '.$row1['description'].' </option>';  
         }
     ?>
    </select>
</div>

I expect the list from the DB to be in the dropdown, but it is empty (No dropdown list).

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

0

Get rid of $row1 = mysqli_fetch_array($result1); before your while loop and replace the loop with this...

while($row1 = mysqli_fetch_array($result1))  
{  
    ?>
        <option value="<?php echo $row1['code']; ?>"><?php echo $row1['description']; ?></option> 
    <?php
}

Echoing HTML isn't a good practice. Break it up with PHP tags when possible, it'll make your code much easier to debug.

jtylerm
  • 482
  • 4
  • 15
0

delete your first

$row1 = mysqli_fetch_array($result1);

line & put your two variables(result & row1) in var_dump to get debug

Nima Ka
  • 163
  • 10