How to update database with entire html table (PHP, MySQL)
<p>I have been struggling with this for several days now. I have searched on how to update tables and have managed to get as far as to update rows, but only the last one in the table. So now i am trying to get a loop that loops through all the inputs and updates the database with the inputted values. I think the code that needs to be corrected is located near the end of the code</p>
<p>What i want to do:</p>
<ul>
<li>Get/display database in html table</li>
<li>Change values of certain columns</li>
<li>Update the database table using a submit button which updates every row in database</li>
</ul>
<p>Here is a picture of what the table looks like in web view:</p>
<p><a href="https://i.stack.imgur.com/045ny.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/045ny.png" alt="Table as it would look in web view" /></a></p>
<pre><code>&lt;?php
//Connect to database
include '../db/connect.php';
?&gt;
   &lt;form action='test7.php' method=&quot;post&quot;&gt;
      &lt;table border='1'&gt;
         &lt;?php
            $result = $MySQLi_CON-&gt;query(&quot;SELECT * FROM users&quot;);
               echo &quot;&lt;tr&gt;&quot;;
               echo &quot;&lt;td colspan='3'&gt;CLASS 1&lt;/td&gt;&quot;;
               echo &quot;&lt;/tr&gt;&quot;;
               //All table rows in database presented in html table
               while($row = $result-&gt;fetch_array()){
                  echo &quot;&lt;tr&gt;&quot;;
                  echo &quot;&lt;td&gt;&lt;input type='hidden' name='user_id[]' value='&quot;.$row['user_id'].&quot;' /&gt;&lt;/td&gt;&quot;;
                  echo &quot;&lt;td&gt;username  :&lt;input type='text' name='username[]' value='&quot;.$row['username'].&quot;' /&gt;&lt;/td&gt;&quot;;
                  echo &quot;&lt;td&gt;email  :&lt;input type='text' name='email[]' value='&quot;.$row['email'].&quot;' /&gt;&lt;/td&gt;&quot;;
                  echo &quot;&lt;td&gt;rank  :&lt;input type='number' name='rank[]' value='&quot;.$row['rank'].&quot;' /&gt;&lt;/td&gt;&quot;;
                  echo &quot;&lt;/tr&gt;&quot;;
               }
            echo &quot;&lt;input type='submit' name='update' value='UPDATE' /&gt;&quot;;
         ?&gt;
      &lt;table&gt;
   &lt;/form&gt;
&lt;?php
   if(isset($_POST['update'])){ 
      $total = count($_POST['rank']); 
      $user_id_arr = $_POST['user_id']; 
      $rank_arr = $_POST['rank']; 
      for($i = 0; $i &lt; $total; $i++){ 
         $user_id = $user_id_arr[$i]; 
         $rank = $rank_arr[$i]; 
         $query = &quot;UPDATE users SET `rank`= '&quot;.$rank.&quot;' WHERE `user_id`= '&quot;.$user_id.&quot;'&quot;; 
         $MySQLi_CON-&gt;query($query); 
         header('Location: test7.php');
      } 
   }
?&gt;
</code></pre>
<p>When I press the UPDATE button, i get <code>PHP Notice:  Array to string conversion in...</code>.</p>
<p>It refers to line 30 which is this line:</p>
<p><code>$query = &quot;UPDATE user SET rank=$_POST[rank][$row] WHERE user_id=$value &quot;;</code></p>
<p>EDIT: Edited the code above to the working code. Thank you @Frayne Konok for your help.</p>