Farhad paikan Farhad paikan - 1 year ago 68
MySQL Question

Output data from database with PHP into dropdown box

i have a form designed with bootstrap style and i want retrieve data from database into drop down list i tried this code but i get a list with no text.

<div class="form-group"><label class="col-sm-2 control-label">Location</label>
<div class="col-sm-8"><select class="form-control m-b" name=Location>
$mysqli = new mysqli( 'localhost', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db' );
/* check connection */
if (mysqli_connect_errno())
printf("Connect failed: %s\n", mysqli_connect_error());
$query = "SELECT * FROM adhoc";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
echo "<option value=" . $rows['Aim'] . "></option>";

echo "<option value='{".$d['Aim']."}'>".$d['Aim']."</option>";

Ben Ben
Answer Source
  1. $d is never mentioned before you try to echo it, so what is it's content?
  2. echo "<option value=" . $rows['Aim'] . "></option>"; has two issues: firstly, the value is not encased in quotes, and secondly, there is no content in the tag, so no text will output.

Try changing your while to:

while($rows = mysqli_fetch_assoc($result)){
    echo "<option value='{$rows['Aim']}'>{$rows['Aim']}</option>";

As long as $rows['Aim'] contains the content you wish to output, this should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download