title: PHP dropdown inside table
body: <p>I have a table where all the values are selected from the database, but some cells are empty. How  do i put a dropdown list inside that empty cell. The values from the dropdown must come from the database</p>
  
  <p>This is my code:</p>
  
  <pre><code>    &lt;?php
  include("css/style.php");
  
  /* Attempt MySQL server connection. Assuming you are running MySQL
  server with default setting (user 'root' with no password) */
  $link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");
  
  // Check connection
  if ($link === false) {
      die("ERROR: Could not connect. " . mysqli_connect_error());
  }
  
  $dropdown_list = '';
  $sql = "SELECT * FROM orden";
  $result_list = mysqli_query($link, $sql);
   if (mysqli_num_rows($result_list) &gt; 0) {
       $dropdown_list = '&lt;select&gt;';
       while ($row = mysqli_fetch_array($result_list)) {
           unset($id, $name);
           $id = $row['id'];
           $name = $row['id'];
           $dropdown_list .= '&lt;option value="' . $id . '"&gt;' . $name . '&lt;/option&gt;';
  
       }
        $dropdown_list .= '&lt;/select&gt;';
   }
  
  // Attempt select query execution
  $sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
  if ($result = mysqli_query($link, $sql)) {
      if (mysqli_num_rows($result) &gt; 0) {
          echo '&lt;form method="POST"&gt;';
          echo "&lt;table&gt;";
          echo "&lt;tr&gt;";
          echo "&lt;th&gt;Norm id&lt;/th&gt;";
          echo "&lt;th&gt;Norm&lt;/th&gt;";
          echo "&lt;th&gt;Omschrijving&lt;/th&gt;";
          echo "&lt;th&gt;Clusteren&lt;/th&gt;";
          echo "&lt;th&gt;Ordenen&lt;/th&gt;";
          echo "&lt;/tr&gt;";
  
          while ($row = mysqli_fetch_array($result)) {
              if ($row['orden_name']) {
                  $data_list = $row['id'];
              } else {
                $data_list = $dropdown_list;
              }
              echo "&lt;tr&gt;";
              echo "&lt;td&gt;" . $row['norm_id'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $row['norm_name'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $row['description'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $row['cluster_name'] . "&lt;/td&gt;";
              echo "&lt;td&gt;" . $data_list . "&lt;/td&gt;";
              echo "&lt;/tr&gt;";
  
          }
          echo "&lt;/table&gt;";
  
          echo '&lt;input type="submit" &lt;/input&gt;&lt;form&gt;';
          // Free result set
          mysqli_free_result($result);
      } else {
          echo "No records matching your query were found.";
      }
  
  } else {
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
  }
  
  if(isset($_POST['submit']))
  {
    $sql = "INSERT INTO norm (orden_id) VALUES ('$data_list')";
    if(mysqli_query($link, $sql)){
      echo "Records added successfully.";
  } else{
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
  }
  }
  // Close connection
  mysqli_close($link);
  ?&gt;
  </code></pre>