I have a dropdown list which is populated by fetching data from a table in the database. Is it possible to show the same dropdown list for editing with the option selected (value existed in the database) on an editing page and if the user changes and selects other option, on submitting such option can be inserted in the table.

I haven’t tried anything, so far as I don’t know how to do it.

<?php
$conn = new mysqli('localhost', 'root', '', '') 
or die ('Cannot connect to db');
$result2 = $conn->query("select id, username, clerkcode, post, case when 
post='Law Clerk' then 1 when post='Law Intern' then 2 else 3 end priority 
from lawclerk order by priority, clerkcode");  
echo "<html>";
echo "<body>";
echo "Clerk Name : &nbsp;&nbsp; &nbsp;";
echo "<select name='search' Id='search' required />";
echo '<option value="">Select Law Clerk/Intern</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $clerkcode, $username);
$id = $row['id'];
$clerkcode = $row['clerkcode'].' ';
$username = $row['username'];
echo '<option value="'.$clerkcode.'">'.$clerkcode.$username.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>

The result desired: If the user opens the Editing Form a dropdown list would show the option existed in the database as selected with all other options in it so that user can change it, in the case so required.

1 Answers

0
Bhavik On

You can check for value in option tag. <option value="'.$clerkcode.'"'; if($clerkcode=="Your value") { echo ' selected '; } echo '>'.$clerkcode.$username.'</option>