title: How can we process a dynamic form in php
body: <p>The newbie is back with another question. Any help would be much appreciated. <strong>Suppose</strong> we have got a form in which we have written down the name of a user and in front of which there is an input box in which we can allocate a grade to the mentioned user. Within this scenario, everything is clear. We have a form with the name of user (it's 'id' as the value) and another variable, that is the grade' which are posted to the php-action-page. Hence, in the php-action-page, I get two variables, one is the id of the user and the other allocated grade, through POST. Here, everything is clear and the process easy, since I have got just two defined variables. Now, <strong>suppose</strong> that we are inserting a list of users from our 'Users' table into the form dynamically. We fill our form with for example 10 users grabbed from the database. In front of them there are input boxes for the 'grade' to be inserted into. So far, everything is fine. The problem, though, lies in the next stage. <strong>The problem</strong> is I don't know how to ask php-action-page to do the insert, that is insert the grade in the database for specific users as long as there are posted variables of users. Here I have tens of users and tens of dynamic variables. <em>And</em> if the question is a little bit vague, please do excuse me; yet, do your best to get me free from this condition of bafflement. Many thanks.</p>
  
  <p>Here comes some bits of the code to make the problem a little more clear.
  I start with the following code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  require_once ('../inc/takein.php');
  $pd = new dbase();
  $students = $pd-&gt;run_a_query('SELECT * from `checking`');</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here I am including the database and other necessary files. Then I run a query to fetch a list of my students from the table. So far, everything is fine. The next line of action which makes me perplexed is the following code. 
  Before having a look at the code may you please look at the html design in the following picture:
  <a href="https://i.stack.imgur.com/IxR0T.png" rel="nofollow noreferrer">Final Design</a>
  <br>I totally have no idea about it being wrong or correct. You might help with this bit as well.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>    &lt;form action="grades.php" method="post"&gt;
          &lt;table class="table table-bordered font-label" id="item_table"&gt;
  &lt;?php
  foreach ($students as $student) {
      echo '&lt;tr&gt;';
      echo '&lt;td&gt;'.$student['name'].'&lt;/td&gt;';
      echo '&lt;td&gt;&lt;input type="text" name="grade[]" class="form-control omit-radius-input"&gt;&lt;/td&gt;';
      echo '&lt;input type="hidden" name="id[]" value="'.$student['id'].'"&gt;';
      echo '&lt;tr&gt;';
  }
  ?&gt;
          &lt;/table&gt;
  &lt;input type="submit" name="dispatched" class="btn btn-green"&gt;
          &lt;/form&gt;</code></pre>
  </div>
  </div>
  
  <br>Here, I am putting the information in a table within the form element. As you can see in the above picture, I am getting four students from the database. Now I want to send these students back to the database along with their newly set grades. What I want to be posted here is the student id and their grades.<br>
  Then, the following is the last part of the code which is left incomplete because I couldn't make any senses how to do it.<br></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>if (isset($_POST['dispatched'])) {
      $id[] = $_POST['id'];
      $grade[] = $_POST['grade'];
      // what to do now???!!!
      foreach(...HOW TO DO THE 'FOREACH') {
      ...
      }
  }</code></pre>
  </div>
  </div>
  
  <br> May you please help me insert my student grades. Many thanks in advance.</p>