[Solved] MySQL and PHP Select Option with information from database


I’ll give you a short example.

Right now, you’r code will give you 1 option

<select name ="Employee Name" style="width: 160px" >
<option value ="">Please select ...</option></select>

Let’s take an array like:

$array = array('0' => 'test', '1' => 'test1');

To populate your array as options, you can simply do

<select>
<?php
foreach ($array as $a) {
?> 
<option value=""><?= $a ?></option>
<?php
}
?>
</select>

which will give you all values from your array as an option.


You can now go and fill your array with data from your db by

$con = new mysqli('HOST', 'DB_USER' , 'DB_PASS' , 'DB');
$sql = "SELECT * FROM TABLE");
if ($result = $con->query($sql)) {
while ($row = $result->fetch_assoc()) {
$array = array('0' => $row['row1'], '1' => $row['row2']);
}}

Now you have filled your array with the data from db and can populate this in your options as you want.

2

solved MySQL and PHP Select Option with information from database