title: What is the correct foreach expression to update mysql rows from this while statement?
body: <p>I have found similar questions including one on stackoverflow here: <a href="https://stackoverflow.com/questions/16116051/update-echoed-data-using-while-loop-only-updates-one-record">Update echoed data using WHILE loop. Only updates one record</a></p>
  
  <p>I have been having a problem using these examples for my use and am looking for a specific foreach expression to use. I'm sure the solution is very simple.</p>
  
  <p>Table "test":</p>
  
  <pre><code>+--------+
  |id|text |
  +--+-----+
  |0 |Text1|
  +-+------+
  |1 |Text2|
  +-+------+
  |2 |Text3|
  +-+------+
  |4 |Text4|
  +-+------+
  |5 |Text5|
  +-+------+
  |6 |Text6|
  +-+------+
  |7 |Text7|
  +-+------+
  </code></pre>
  
  <p>A PDO prepared statement is executed with a select query selecting all the rows in the table "test". The rows are fetched in a while statement and each row is placed into inputs for the user to change the "text" field data as so:</p>
  
  <pre><code>$sth = $db-&gt;prepare("SELECT * FROM test ORDER BY id");
  $sth-&gt;execute();
  
  echo '&lt;form class="ajax" method="post"&gt;';
  
  while($row = $sth-&gt;fetch()) {
  echo '&lt;input type="text" name="id" class="id" value="'.$row['id'].'" /&gt;
  &lt;input type="text" name="text" class="text" value="'.$row['text'].'" /&gt;'; 
  }
  
  echo '&lt;input type="submit" name="submit" value="submit"&gt;&lt;/form&gt;';
  </code></pre>
  
  <p>On submit the database is (supposed to) update all text fields by the id.</p>
  
  <pre><code>if (isset($_POST['submit'])) {  
    $text = $_POST['text'];
    $id = $_POST['id'];
    $sql = "UPDATE test SET text = :text WHERE id = :id";
    $sth = $db-&gt;prepare($sql);
    $sth-&gt;execute(array(':text'=&gt;($text),':id' =&gt; ($id)));
  }
  </code></pre>
  
  <p>Currently only the last row is updated in the database and I believe that I need to place my prepared update statement in a foreach loop.</p>
  
  <p>Something like this:</p>
  
  <pre><code>foreach($something as $something =&gt; $somethingElse) {
    $text = $_POST['text'];
    $id = $_POST['id'];
    $sql = "UPDATE test SET text = :text WHERE id = :id";
    $sth = $db-&gt;prepare($sql);
    $sth-&gt;execute(array(':text'=&gt;($text),':id' =&gt; ($id)));
  }
  </code></pre>
  
  <p>What is the correct expression to use here? Or, am I barking up the wrong tree in some way?</p>
  
  <p>Thanks</p>
  
  <p>Sam</p>