title: Update mysql database fields with array php
body: <p>I'm trying to achieve a multiple update in one submit. I have a table with a number of rows and want to be able to update one field in each row just by tabbing to the next insert box.</p>
  
  <p>My code is thus:-
      //start a table
      echo '
      ';</p>
  
  <pre><code>//start header of table
  echo '&lt;tr&gt;
  
  &lt;td width="60" align="center"&gt;&lt;strong&gt;Lab Item ID&lt;/strong&gt;&lt;/td&gt;
  &lt;td width="60" align="center"&gt;&lt;strong&gt;Test Suite Name&lt;/strong&gt;&lt;/td&gt;
  &lt;td width="60" align="center"&gt;&lt;strong&gt;Test Name&lt;/strong&gt;&lt;/td&gt;
  &lt;td width="50" align="center"&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/td&gt;
  &lt;/tr&gt;';
  
  //loop through all results
  while ($row=mysql_fetch_object($sql)) {
      //print out table contents and add id into an array and email into an array
      echo '&lt;tr&gt;
      &lt;td align="center"&gt;&lt;input name="id[]" value='.$row-&gt;lab_item_id.' readonly&gt; &lt;/td&gt;
      &lt;td align="center"&gt;'.$row-&gt;test_suite_name.'&lt;/td&gt;
      &lt;td align="center"&gt;'.$row-&gt;test_name.'&lt;/td&gt;
      &lt;td&gt;&lt;input name="test_result[]" type="text" value="'.$row-&gt;test_result.'"&gt;&lt;/td&gt;
      &lt;/tr&gt;';
  }
  
  //submit the form
  
  echo'&lt;tr&gt;
  &lt;td colspan="3" align="center"&gt;&lt;input type="submit" name="Submit" value="Submit"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;';
  //if form has been pressed proccess it
  if($_POST["Submit"])
  {
  
  //get data from form
  //$name = $_POST['name'];
  
  //$_POST['check_number'] and $_POST['check_date'] are parallel arrays
  
  foreach( $_POST['id'] as $id ) {
       $tresult = trim($_POST['test_result']);
        $query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
        //execute query
  
  }  
   print_r($_POST);
   var_dump($tresult);
  //redirect user
  $_SESSION['success'] = 'Updated';
  //header("location:index.php");
  }
  ?&gt;
  </code></pre>
  
  <p>When I print the $_POST arrays, everything is populating fine, however the variable is Null. I know I can't do a foreach on multiple arrays (at least I don't think I can) so is there some other trick I'm missing please? I can't be far away as the $_Post print has the right data in it.</p>
  
  <p>Incidentally, the whole thing is generated by a query, so I never know how many records I'll have to update.</p>
  
  <p>I've been looking at this (and other) forums, but can't seem to get a solution. I thought I understood arrays, but now I'm beginning to wonder!</p>
  
  <p>edit - it's the $tresult variable that isn't working. </p>
  
  <p>Many thanks,
  Jason</p>
  
  <p>Edit Thursday 21st Feb (05:41 UK time)
  Thanks for your input everybody. I've solved this one now, and your collective advice helped. The code that finally cracked it is:-</p>
  
  <pre><code>//get data from form
  $id1 = $_POST['id'];
  $test_result1 = $_POST['test_result'];
  foreach ($id1 as $key =&gt; $value){
  $query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id=$value ";
  //execute query
  </code></pre>
  
  <p>Working through which variables etc were populated and what they were populated with was the key. Back to first principles, isn't it?</p>
  
  <p>Cheers all.
  J</p>