-1

I want to view some foods which I added through html and php code into database like food a, food b, food c, food d.

I want to view these food category in my dropdown list in another page of html as a updating food category .

<?php
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'rfid'; // Database Name

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
 die ('Failed to connect to MySQL: ' . mysqli_connect_error()); 
}

?>  
  <div class="sign_up">
  
    <div class="formtitle" align="center">View Food Items </div>
    <div class="top_section">
     <div class="section">
     <br>
      <div class="input-sign details2">
      <form name="form1" action="" method="post">
       &nbsp;&nbsp; View Foods: 
         <?php
        if ($result = @$dbconn->query("SELECT * FROM `food`")) 
        {
        //`food` represents the table name.
          echo "<select>";
          while ($row = $result->fetch_assoc()) 
    {
            echo "<option value=\"".$row["food"]."\">".$row["food"]."</option>";
            /*"id" and "foodname" represents the table column name.*/
          }
          echo "</select>";
        }
       ?>
       <select>
        <option><?php echo $row["food"];?></option>
       </select>
       
       </form>
      </div>      
     </div>    
     </div>
 <div class="clear"> </div>
  </div>

2 Answers2

0

So I'm assuming you know how to connect a db and use it.

Here is an example of what you want.

        if ($result = $dbconn->query("SELECT * FROM `food`")) {
          //`food` represents the table name.
          echo "<select>";
          while ($row = $result->fetch_assoc()) {
            echo "<option value=\"".row["id"]."\">".row["foodname"]."</option>";
            /*"id" and "foodname" represents the table column name.*/
          }
          echo "</select>";
        }
TheE
  • 328
  • 4
  • 13
0

It might have something to do with your quotations, please try echo "<option value=\"".$row['food']."\">".$row["food"]."</option>"; In addition, I found this similar questions which may help you solve your problems. Credits to author @SpaceBeers

Xer Charles
  • 251
  • 1
  • 6