Chathurika Chathurika - 6 months ago 20
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.

<?php
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

You can try this :

       $echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
       while($r = mysql_fetch_array($result)) {
          $value=$r['name'];
          $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 : http://php.net/manual/en/book.pdo.php