title: How to update a field in sql table with inputs from an html form against displayed results which are themselves output of an sql query?
body: <p>I have a database that looks like below.</p>
  
  <pre><code>Id Class  Name                Email                           Comments  
    1   4    Thomas Sheffield  tom@email.com  
    2   5    Natasha Rosewhite natasha@email.com  
    3   4    Jerome Manyville  jerry@email.com  
    4   6    Susan Carrey      suzzy@email.com 
    5   4    Selina Waterwall  selina@email.com 
  </code></pre>
  
  <p>I have a php page that would query names from a particular class and display it as an html table.
  The html page would also have an input field against each displayed name where the comments can be entered. After the submit button is hit, the comments against each specific name should be updated in the database.
  I have applied the following logic - the Id which is unique will be carried over to another php page (that is indicated by the action field of the submit button) and then use $_POST to update the comments.</p>
  
  <p>I have used the following code</p>
  
  <pre><code> &lt;?php
  session_start();
  $username = "Debo";
  $password = "123456";
  $hostname = "localhost"; 
  $Idtemp = array();
  $i=0;
  $sql = "SELECT `Id`, `Name`, `Email` FROM `registration` WHERE `Class`=4";
  
  $dbconnect=mysqli_connect($hostname, $username, $password, "test1")
   or die("unable to connect"); 
  
  $result = mysqli_query($dbconnect, $sql);
  
  ECHO "&lt;table border=\"2\"&gt;
    &lt;tr bgcolor=\"#cccccc\"&gt;
    &lt;td width=\"100\"&gt;Id&lt;/td&gt;
    &lt;td width=\"200\"&gt;Name&lt;/td&gt;
    &lt;td width=\"100\"&gt;Email&lt;/td&gt;
      &lt;td align=\"center\" width=\"100\"&gt;COMMENTS&lt;/td&gt;
  &lt;/tr&gt;";
  
  while($row = mysqli_fetch_array($result))
  {   ECHO "&lt;tr&gt;&lt;td width=\"100\"&gt;".$row['Id']."&lt;/td&gt;";
      ECHO "&lt;td width=\"200\"&gt;".$row['Name']."&lt;/td&gt;";
      ECHO "&lt;td width=\"100\"&gt;".$row['Email']."&lt;/td&gt;";
      ECHO "&lt;td align=\"center\"&gt;&lt;input type=\"text\" name=\"comments[]\" size=\"50\"&lt;/td&gt;&lt;/tr&gt;";
      $IDtemp[$i]=$row['Id'];
      $i++;};
  
  
  ECHO"
  &lt;table&gt;
  &lt;form action=\"processregistration.php\" method=\"post\"&gt;
  &lt;tr&gt;
    &lt;td colspan=\"8\" align=\"center\"&gt;&lt;input type=\"submit\" name=\"submit\" value=\"Submit Feedback\"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;";
  
  $_SESSION['CURRENTID']=$IDtemp;
  
  ?&gt;
  </code></pre>
  
  <p>And then on submit, it goes to </p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  $COMMENTS = array();
  $ID = $_SESSION['CURRENTID'];
  var_dump($ID);
  if (isset($_POST['submit']))
  {
  $COMMENTS=$_POST['comments'];
  };
  $username = "Debo";
   $password = "123456";
   $hostname = "localhost"; 
  
  foreach($ID as $key=&gt;$n)
  {
      $sql = "UPDATE  `test1`.`registration` SET `Comments` = $COMMENTS[$key] WHERE `registration`.`UID` = $n";
      var_dump($sql);
  
       $dbconnect=mysqli_connect($hostname, $username, $password, "test1")
      or die("unable to connect");
      mysqli_query($dbconnect, $sql);
  if (mysqli_error($dbconnect))
  {
      ECHO "Error Description:".mysqli_error($dbconnect);};
  
  };
  
  ?&gt;
  </code></pre>
  
  <p>I find that the 'comments' are not getting posted.
  I get the notice 'undefined index: comments' on line 10.</p>
  
  <p>What am I doing wrong?
  I am pretty new to php. Please help. Also, the way through which I am trying to achieve what I intend to get, is that correct or is there a better approach?</p>
  
  <p>Regards.</p>