0

I am trying to fetch values from my database and display the selected value in a dropdown list with other values. I have seen many questions here regarding this issue but none of them is working. Please Help!

<select name= "indication_name" id= "update_indication_id" class="form-control" required>
  <?php
      $sql = "SELECT id,description From indications";
      $result=mysqli_query($con,$sql);
      while($row=mysqli_fetch_array($result)){
          echo "<option selected='selected'>" . $row['id']." - ". $row['description'];
      }

   ?>
</select>
3
  • It's not working how..? Commented Oct 22, 2016 at 18:08
  • So... where are you closing the option tag? pretty obvious here. Commented Oct 22, 2016 at 18:09
  • I want the value from the database to be the value of the value of my select Commented Oct 22, 2016 at 18:15

3 Answers 3

1

There are three main errors I can see:

  1. You're making every option selected like that.

  2. The options don't have a value.

  3. And the option tags should be closed.

So it would be like this:

<select name= "indication_name" id= "update_indication_id" class="form-control" required>
  <?php
      $sql = "SELECT id,description From indications";
      $result=mysqli_query($con,$sql);
      while($row=mysqli_fetch_array($result))
          echo "<option value='" . $row['id'] . "'>" . $row['description'] . "</option>";
   ?>
</select>

Another thing, that isn't an error, but I personally think is good, is having a "unselected" option:

<select name= "indication_name" id= "update_indication_id" class="form-control" required>
  <option selected="selected" value="">-- Select an option --</option>
  <?php
      $sql = "SELECT id,description From indications";
      $result=mysqli_query($con,$sql);
      while($row=mysqli_fetch_array($result))
          echo "<option value='" . $row['id'] . "'>" . $row['description'] . "</option>";
   ?>
</select>

The value of that option should be null, so that required applies to it.

Sign up to request clarification or add additional context in comments.

1 Comment

but i need the value from my database. an that is my main problem. when my id=1 i want it to show me x. so x should in my textbox. wven if i have 7 ids
0
<?php 
$server="localhost";
$username="root";
$password="";
$link=@mysql_connect($server, $username, $password) or die ("Cannot connect to mysql server: ".@mysql_error());
$dbname = 'database_name';
@mysql_select_db($dbname, $link) or die ("Cannot connect to database: ".@mysql_error());

$query="SELECT id,description from indications";
$result = @mysql_query ($query);
echo "<select name=indication_name value=' '>";
while($drop=@mysql_fetch_array($result)){
echo "<option value=$drop[id]>$drop[description]</option>";
}
echo "</select>";
?>

Comments

0

Try using switch case and put the value in switch from the database and then set selected ='selected' if the case value matches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.