-2

Please how do i populate select dropdown list directly from Database table colunm?

Looking forward to your answers. Thanks

<?php 
include('header.php');
include('server.php');//DB Connection
?>

<div class="form-group">
 <label>Select Center</label>
            <?php
                echo "<select class='form-control' name='center'>";
                $result = mysqli_query("SELECT center_name FROM center");

while ($row = mysqli_fetch_assoc($result)) {
                    unset($center);
                    $center = $row['center']; 
                    echo '<option value="'.$center.'"></option>';

                    }

                    echo "</select>";
            ?>
</div>
<div>
<button type="submit" class="btn btn-primary btn-block" name="submit" value="submit">Submit</button>

2 Answers2

1

You have used column name center_name but under loop you put center as array key

Change

$center = $row['center']; 

to

$center = $row['center_name'];

And you have set option values but you forgot to add your php variable inside option tag

echo '<option value="'.$center.'">'. $center .'</option>';
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
0

Thanks all. I resolved the issue by viewing the HTML sourescode through the browser and discovered an error message in the viewsource on browser which did not display to me.

The solution was that i just passed the Database connection as $con as second parameter into the mysqli_query() and that solved the problem.

Thanks all. <div class="form-group"> <label>Select Center</label> <select class='form-control' name='center_name'><br /> <b>Warning</b>: mysqli_query() expects at least 2 parameters, 1 given in <b>C:\xampp\htdocs\soap\create_user.php</b> on line <b>141</b><br /> <br /> <b>Warning</b>: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in <b>C:\xampp\htdocs\soap\create_user.php</b> on line <b>143</b><br /> </select>
</div> <div>