简体   繁体   中英

how to make the value fetched from mysql as selected option in a drop down using php or javascript

I'm getting the details from the user and storing it in mysql database. I have an update page in which the user can update the values. At that time, I want to allow the user to reselect the value from the drop down in which the value entered at first should be already selected. how can I make the value fetched from the database as selected option. please explain with some code samples.

$query="SELECT * from tablename where id=".$id;// I've assigned the value for id 
$rows=mysql_query($query,$connect); 
$row=mysql_fetch_array($rows,MYSQL_ASSOC);
?>
<select name="designation"> <?php
   echo "<option value=\"$row[OptionID]\" SELECTED>$row[OptionName]</option>\n";
?>
    <option value=sol1>sol1</option>
    <option value=sol2>sol2</option>
    <option value=sol3>sol3</option>
</select> 

In order to make a dropdown select box using a database you can follow the below given approach:

<?php
 $databaseHost = "localhost"; 
 $databaseUser = "root";
 $databasePassword = "password";
 $databaseName = "employee";
 $con=mysql_connect($databaseHost ,$databaseUser ,$databasePassword,'employee')or die ('Connection Error');
$dbSelected = mysql_select_db('foo', $con);
if (!$dbSelected) {
    die ('Can\'t use foo : ' . mysql_error());
}
 ?>
<strong> Select Designation : </strong> 
<select name="empName"> 
    <option value=""> -----------ALL----------- </option> 
    <?php
         $dd_res=mysqli_query($con, "Select DISTINCT designation from emp");
         while($r=mysqli_fetch_row($dd_res))
         { 
               echo "<option value='$r[0]'> $r[0] </option>";
         }
     ?>
</select>

UPDATE For old MySQl:

<?php
 $databaseHost = "localhost"; 
 $databaseUser = "root";
 $databasePassword = "password";
 $databaseName = "employee";
 $con=mysql_connect($databaseHost ,$databaseUser ,$databasePassword)or die ('Connection Error');
$db_selected = mysql_select_db('foo', $con);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
 ?>
<strong> Select Designation : </strong> 
<select name="empName"> 
    <option value=""> -----------ALL----------- </option> 
    <?php
         $dd_res=mysql_query("Select DISTINCT designation from emp");
         while($r=mysql_fetch_row($dd_res))
         { 
               echo "<option value='$r[0]'> $r[0] </option>";
         }
     ?>
</select>

Making an assumption that you have a table with users (ID, UserID, OptionID) and a table of options (OptionID, OptionName) or similar.

$get_details = $conn->query("SELECT * FROM table");
$data = $get_details->fetch_assoc();

$get_options = $conn->query("SELECT * FROM options");
while($row = $get_options->fetch_assoc()) {  
  if($row['OptionID'] == $data['OptionID'])
    echo "<option value=\"$row[OptionID]\" SELECTED>$row[OptionName]</option>\n";
  else
    echo "<option value=\"$row[OptionID]\">$row[OptionName]</option>\n";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM