title: How to store array input into database? MYSQL PHP form input foreach
body: <p>In this project, I am making an attendance user interface for the teacher. Here teacher can update student's marks which will directly update into the MYSQL database. Below shows the code that I used to echo out the list of students from the database. Here, I have used array i.e. name="gradeEdit[]" in input section. </p>
  
  <p>teacherGrades.php</p>
  
  <pre><code>&lt;?php
  $con=mysqli_connect("#", "#", "", "#");
  if (mysqli_connect_errno())
   {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
  
  $result = mysqli_query ($con,"SELECT ");
  echo ' &lt;form method="POST" action="teacherGrades.php"&gt; 
    &lt;table class="tableEchoPupil" border="1"&gt;
  &lt;tr&gt;
  &lt;th&gt;Name&lt;/th&gt;
  &lt;th&gt;Edit&lt;/th&gt;
  &lt;/tr&gt;';
  
  while($row = mysqli_fetch_array($result))
  
  {
  echo "&lt;tr&gt;
  &lt;td&gt;" . $row['name'] . "&lt;/td&gt;
  &lt;td&gt; &lt;input type='text' name='gradeEdit[]' /&gt; &lt;/td&gt; 
  &lt;/tr&gt;"; }
  
  echo "&lt;input class='gradeSubmit' type='submit' name='btnSubmit' value='Submit'&gt;"; 
  echo "&lt;/table&gt;"; 
  echo "&lt;/form&gt; &lt;br/&gt;"; 
   ?&gt;
  </code></pre>
  
  <p>Below shows the code that is stored in the beginning of this page [teacherGrades.php
  ]. When the teacher inputs new grades and click the submit button from the previous code, the grades has to updated in the database. However the problem is that, I am not able to update it properly. I think there is a few problem behind my code, can you please check and help me. If there are further questions, I am ready to answer. </p>
  
  <p>Thank you in advance. </p>
  
  <pre><code> &lt;?php 
   $con=mysqli_connect("#","#","","#");
   if (mysqli_connect_errno()) {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   if ($_POST['btnSubmit']){
  
   $gradeEdit = $_POST['gradeEdit']; 
  
      foreach ($gradeEdit as $key =&gt; $value) {
  
        $grades = implode(',', $gradeEdit);
  
        $query = "UPDATE classroom_student_teacher SET marks = '$grades'
   WHERE teacher_id = $userid AND classroom_id = $id";
  
    $close = mysqli_query($con, $query); 
    }
    }
     ?&gt; 
  </code></pre>