Skip to content
Advertisement

PHP Dropdown Box from SQL Join

I want to do is have a dropdown box that will display the MealOption and when that is selected somehow find that products meal id?

This is my database layout This is my database layout

Advertisement

Answer

<?php 
public function get_data()
{
   $mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't    connect".mysqli_connect_error());
   $sql="select id,name from table";
   $result=$mysqli->query($sql) or die($mysqli->error);
      while ($row=$result->fetch_array(MYSQLI_ASSOC)) 
      {
        echo "<option value="".$row["id"].""  selected>".$row["name"]."</option>";
      }
 }  
?>
<select name="abc" id="xyz">
<?php  get_data(); ?>
</select>

In the function connect the db and then run the query. in the while loop echo your rows like above so you can get your name and id from table. In the end in your html call this function.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement