Hi

I am retrieving values from database for edit profile...

I have one query ..... i.e.

Based on department Id in database i am showing selected value in drop down box.... but while inserting how to insert department id.

$data = @mysql_query("select * from Department");
$array = array();
while ($row = mysql_fetch_assoc($data))
{
$id = $row['dept_id'];
$dname = $row['department_name'];
   echo '<option value='.$did.'>'.$dname.'</option>'."\n";

}

In above code $did is selected value from database..... but while inserting how to get $id .......can we write to values in option field?

Thanks in advance

Recommended Answers

All 4 Replies

Your code is almost OK. I only added <select> and </select> tags on the beginning and end of the while loop and double quotes around $id (and changed $did in your code to $id).

$data = @mysql_query("select * from Department");
$array = array();

echo '<select>';

while ($row = mysql_fetch_assoc($data))
{
    $id = $row['dept_id'];
    $dname = $row['department_name'];

    echo '<option value="'.$id.'">'.$dname.'</option>'."\n";
}

echo '</select>';

This not my problem ....I already keep select....I retrieve values in to drop down....I am doing edit profile.... suppose user selects a department electrical while registering to website and after login he want to edit department...at that time i want to show selected value as electrical in drop down box.

I can able do these....but there is a problem ....that is how can i use to option values in drop down box...like one value already selected .....and other value to insert in database?

if you're replacing department display the current(selected) value in a text box
and let users select a new department from the drop down

you got typo error, you type $did instead of $id
changed this code

echo '<option value='.$did.'>'.$dname.'</option>'."\n";

to

echo '<option value='.$id.'>'.$dname.'</option>'."\n";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.