Chathurika Chathurika - 2 years ago 75
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.

Answer Source

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 :

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