Chathurika Chathurika - 11 months ago 42
PHP Question

Retrieve value from DB and display in a drop down on edit form in php

This script does not display the DB value in a drop down on the edit form.

echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
echo "</select>";

It does not show any error. How it can write in a correct way.


You can try this :

       $echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
       while($r = mysql_fetch_array($result)) {
          $echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
       $echoSting .= '</select>'.PHP_EOL;
       echo $echoSting;

a side note, try looking into PDO for your database stuff :