简体   繁体   中英

How can i echo value from database INTO html <select> tag?

How do I echo values that are submitted from a form on another page (which they are submitted into the database) into an HTML tag as options?

This is my latest code and I'm still stuck here. When I clicked on the drop-down list, it still shows nothing.

<select name="comName" id="comName" class="form-control" required>
  <?php include('db_company.php');
  $query_option = "SELECT * FROM company";
  $result = mysqli_query($query_option);

   while($row = mysqli_fetch_array($result)) {
     echo "<option value='{$row['comName']}'>{$row['comName']}</option>";
   }
 ?>
</select>

There are some mistakes that you have done in here.First of all I would like to suggest you to start using error reporting using,

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1'); 
error_reporting(E_ALL); 

If you have used error reporting from the beginning I believe you would have noticed all the issues that you are facing now.

1.You need to use the DB connection when you are using mysqli_query

 $link = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DATABASE);
  $result = mysqli_query($link,$query_option);

2.You are using the wrong value inside the while loop.

while($row = mysqli_fetch_array($result)) {
         echo "<option>".$row{'company'}."</option>";
    }

You are getting comName from the query so you have to use comName instead of company .

while($row = mysqli_fetch_array($result)) {
     echo "<option value=".$row{'comName'.">".$row{'comName'}."</option>";
}

Try the below code it's work proper.

<select name="comName" id="comName" class="form-control" required>
    <?php include('db_company.php');
        $query_option = "SELECT * FROM company";
        $result = mysqli_query($query_option);

        while($row = mysqli_fetch_array($result)) {
            echo "<option value='".$row['comName']."'>".$row['comName']."</option>";
        }
    ?>
</select>

Your echo in row is wrong

<?php include('db_company.php');

  $query_option = "SELECT * FROM company";
  $result = mysqli_query($query_option);

  while($row = mysqli_fetch_array($result)) {
    echo "<option value='{$row['id']}'>{$row['company']}</option>";
  }

?>

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