title: Foreach loop updating every database value
body: <p>Good afternoon,</p>
  
  <p>I am building a journal, in which I have separate divs. <br />
  Each div element displays a different log, with data specific to each log in the div. <br />
  For each of the logs, I have a submit button that allows the form to be updated and edited. <br /></p>
  
  <p>What is happening, is that when I submit the form, it is updating each entry in my database, rather than just 1 single entry. 
  I know that the trouble is with my foreach loop, but I am unsure how to remedy this. </p>
  
  <p>Any assistance or pointers would be greatly appreciated.</p>
  
  <p>Code below: <br/></p>
  
  <pre><code>&lt;?php 
      $sqlGrow = "SELECT * FROM grow_details ";
      $query = $conn-&gt;query($sqlGrow);
  
      $grows = array();
  
       while ($grow = mysqli_fetch_assoc($query) ) {
          $grows[] = $grow;
      } 
  
      foreach($grows as $grow) {
          $id = $grow['id'];
          $growName = $grow['name'];
  ?&gt;
      &lt;div class="container"&gt;
          &lt;div class="details"&gt;
              &lt;h2&gt;&lt;?php echo $grow['name']; ?&gt;&lt;/h2&gt;
              &lt;p class="growNum"&gt;Grow #: &lt;?php echo $id; ?&gt;&lt;/p&gt;
              &lt;table class="growDetails"&gt;
                  &lt;form method="POST" action=""&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="datePlanted"&gt;Date Planted:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_datePlanted" id="edit_datePlanted" value="&lt;?php echo $grow['datePlanted']; ?&gt;" /&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="strain"&gt;Strain:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_strain" id="edit_strain" value="&lt;?php echo $grow['strain']; ?&gt;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="toMaturity"&gt;Days to mature:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_toMaturity" id="edit_toMaturity" value="&lt;?php echo $grow['toMaturity']; ?&gt;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="type"&gt;Type:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_type" id="edit_type" value="&lt;?php echo $grow['type']; ?&gt;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="gender"&gt;Gender:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_gender" id="edit_gender" value="&lt;?php echo $grow['gender']; ?&gt;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="medium"&gt;Medium:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_medium" id="edit_medium" value="&lt;?php echo $grow['medium']; ?&gt;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="watts"&gt;Watts:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_watts" id="edit_watts" value="&lt;?php echo $grow['watts']; ?&gt;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td class="label_growDetails"&gt;&lt;label for="lightType"&gt;Light Type:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="edit_lightType" id="edit_lightType" value="&lt;?php echo $grow['lightType']; ?&gt;" /&gt;&lt;/td&gt;
                          &lt;td class="edit"&gt;&lt;input type="submit" name="submit_editGrowDetails" id="submit_editGrowDetails" value="&amp;nbsp;&amp;nbsp;&amp;nbsp;Save Edits&amp;nbsp;&amp;nbsp;&amp;nbsp;" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/form&gt;
              &lt;/table&gt;
  
          &lt;/div&gt;
  </code></pre>
  
  <p>And the PHP for the submit button: </p>
  
  <pre><code>&lt;?php
  if(isset($_POST['submit_editGrowDetails'])){
      $edit_datePlanted = mysqli_real_escape_string($conn, $_POST['edit_datePlanted']);
      $edit_strain = mysqli_real_escape_string($conn, $_POST['edit_strain']);
      $edit_toMaturity = mysqli_real_escape_string($conn, $_POST['edit_toMaturity']);
      $edit_type = mysqli_real_escape_string($conn, $_POST['edit_type']);
      $edit_gender = mysqli_real_escape_string($conn, $_POST['edit_gender']);
      $edit_medium = mysqli_real_escape_string($conn, $_POST['edit_medium']);
      $edit_watts = mysqli_real_escape_string($conn, $_POST['edit_watts']);
      $edit_lightType = mysqli_real_escape_string($conn, $_POST['edit_lightType']);
  
      $name = $grow['name'];
  
      $edit_growDetails = "UPDATE grow_details
                   SET datePlanted = '$edit_datePlanted', 
                      strain = '$edit_strain', 
                      toMaturity = '$edit_toMaturity', 
                      type = '$edit_type', 
                      gender = '$edit_gender', 
                      medium = '$edit_medium', 
                      watts = '$edit_watts', 
                      lightType = '$edit_lightType' 
                   WHERE name = '$name'; ";
  
      $query_edit_growDetails = $conn-&gt;query($edit_growDetails);
  
      if($query_edit_growDetails) {
          echo '&lt;p class="success" id="success"&gt;Successfully updated log for '.$name.'! &lt;a class="refresh" href="journal.php"&gt;Refresh&lt;/a&gt;&lt;/p&gt;';
      } else {
          echo '&lt;p class="error" id="error"&gt;There was an error: '. $conn-&gt;error .'&lt;/p&gt;';
      }
  }
  ?&gt;
  </code></pre>