title: Comparing values from multiple text boxes with mysql table
body: <p>I am trying to implement a form in which a user is asked some questions which are stored in a database. The answers for the respective questions is also stored and the user to supposed to answer those in the text boxes provided (like an online test system). I want to store all the answers entered by the user in an array, while storing the values of the answer column of the database in another array. Comparing the values of the two arrays should give me the number of right answers. I referred to this <a href="https://stackoverflow.com/questions/15363787/submit-values-from-multiple-textboxes-as-arrays">question</a> and have used the following codes. But the problem is that the result is showing an undefined offset error at the place where I use variable <code>$i</code> to compare the two arrays. Please help me.</p>
  
  <p><strong>Code for form (giveexam.php)</strong>:</p>
  
  <pre><code>&lt;form id ="answersheet" name="answersheet" action="assessment.php" method="post"&gt;
  &lt;table border="1px"&gt;
  &lt;tr&gt;
  &lt;!--td&gt;sr_no&lt;/td--&gt;
  &lt;td width="144"&gt;Question&lt;/td&gt;
  &lt;td width="50"&gt;option1&lt;/td&gt;
  &lt;td width="50"&gt;option2&lt;/td&gt;
  &lt;td width="50"&gt;option3&lt;/td&gt;
  &lt;td width="50"&gt;option4&lt;/td&gt;
  &lt;td width="30"&gt;
  &lt;strong&gt;ANSWER&lt;/strong&gt;&lt;/td&gt;
  
  
  &lt;/tr&gt;
  &lt;?php
  require_once("connection.php");
  
  $query = "select * from mgmt";
  $result = mysql_query($query, $connection);
  
  if(!$result)
  echo "ERROR: No results";
  else
  {
  while($row = mysql_fetch_array($result))
  {
  
  ?&gt;
  
  &lt;tr&gt;
  &lt;!--td&gt;&lt;?php echo $row['eid'];?&gt;&lt;/td--&gt;
  &lt;td&gt;&lt;?php echo $row['question'];?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo $row['op1'];?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo $row['op2'];?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo $row['op3'];?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo $row['op4'];?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="ans[]"/&gt;&lt;/td&gt;
  
  &lt;/tr&gt;
  
  
  &lt;?php }
  } //end while
  ?&gt;
  &lt;/table&gt;
  &lt;tr&gt;
  
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="submit" value="submit" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>PHP file (assessment.php)</strong>:</p>
  
  <pre><code>&lt;?php
  session_start();
  $m=0;
  //$c=0;
  
              require_once("connection.php");
  
              $query="select answer from mgmt";
              $result=mysql_query($query, $connection);
  
              $result_array=array();
              while($row=mysql_fetch_assoc($result))
              {$result_array[]=$row;}
  
              $answer=array();
              $answer[]=$_POST["ans"];
              for($i=1;$i&lt;4;$i++)
              {
                  if($answer[$i]==$result_array[$i])
                      $m++;
  
              }
              echo $m;
  
                      ?&gt;
  </code></pre>