title: How To Insert data to database from multiple select list using PHP an MySQL
body: <p>I have a form with many multiple select lists. There are several multiple select lists(blocks) in the form. How can I submit the multiple select data to the database?</p>
  
  <p>I have tried to create a foreach statement but the problem is it only submits one option in the database.</p>
  
  <pre><code>    &lt;?php 
  $db = mysqli_connect('localhost','root','','trial') or die($db);
  if (isset($_POST['submit'])) {
  $marks = mysqli_real_escape_string($db,$_POST['marks']);
  $subjects= $_POST['subject'];
  $farming= $_POST['farming'];
  foreach ($subjects as $i) {
      $subject = $i;
      $sql = "INSERT INTO `trial_table` (`subjects`, `marks`) VALUES ('".mysqli_real_escape_string($db,$subject)."', '$marks')";
  mysqli_query($db,$sql);
  }
  $sql1 = "INSERT INTO `trial_table` (`marks`) VALUES ('$marks')";
  mysqli_query($db,$sql1);
  }
   ?&gt;
  &lt;body&gt;
      &lt;form method="POST" action="test.php"&gt;
      &lt;select id="multiselect" name="farming[]" multiple="multiple" required&gt;
      &lt;option value="Irrigation"&gt;Irrigation&lt;/option&gt;
      &lt;option value="Fertilizer"&gt;Fertilizer&lt;/option&gt;
      &lt;option value="Pesticide"&gt;Pesticide&lt;/option&gt;    
      &lt;/select&gt;
      &lt;select id="multiselect" name="subject[]" multiple="multiple" required&gt;
      &lt;option value="Irrigation"&gt;Technology&lt;/option&gt;
      &lt;option value="Fertilizer"&gt;Science&lt;/option&gt;   
       &lt;/select&gt;
      &lt;div class="input-group"&gt;
          &lt;label&gt;Marks&lt;/label&gt;
          &lt;input type="number" name="marks"&gt;      
      &lt;/div&gt;  
      &lt;button type="submit" name="submit" class="btn"&gt;SUBMIT&lt;/button&gt; 
        &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;`enter code here`
  </code></pre>
  
  <p>I expect to the Form to submit the selected data not just one to the database.</p>