title: how to edit a record from MySQL database
body: <p>this is my first post!</p>
  
  <p>First off, I have my code which outputs a table on index.php. At the end I have an edit link which takes me to the edit.php page:</p>
  
  <pre><code>if ($result-&gt;num_rows &gt; 0) {
      echo "&lt;p&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;ID&lt;/th&gt;&lt;th&gt;Film Name&lt;/th&gt;&lt;th&gt;Producer&lt;/th&gt;&lt;th&gt;Year Published&lt;/th&gt;&lt;th&gt;Stock&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;th&gt;Function&lt;/th&gt;&lt;/tr&gt;";
      while($row = $result-&gt;fetch_assoc()) {
          echo "&lt;tr&gt;&lt;td&gt;".$row["ID"]."&lt;/td&gt;&lt;td&gt;".$row["FilmName"]."&lt;/td&gt;&lt;td&gt;".$row["Producer"]."&lt;/td&gt;&lt;td&gt;".$row['YearPublished']."&lt;/td&gt;&lt;td&gt;".$row['Stock']."&lt;/td&gt;&lt;td&gt;".$row['Price']."&lt;/td&gt;&lt;td&gt;"."&lt;a href=\"edit.php\"&gt;Edit&lt;/a&gt; / Delete"."&lt;/td&gt;&lt;/tr&gt;&lt;/p&gt;";
      }
      echo "&lt;/table&gt;";
  </code></pre>
  
  <p>edit.php (first there is the form):</p>
  
  <pre><code>$query = "SELECT * FROM ProductManagement WHERE ID=" . $_GET["ID"] . ";"; // Place required query in a variable
                  $result = mysqli_query($connection, $query); // Execute query
  
                  if ($result == false) { // If query failed
                      echo "&lt;p&gt;Getting product details failed.&lt;/p&gt;";
                  } else { // Query was successful   
                      $productDetails = mysqli_fetch_array($result, MYSQLI_ASSOC); // Get results (only 1 row 
                      // is required, and only 1 is returned due to using a primary key (id in this case) to 
                      // get the results)
  
                      if (empty($productDetails)) { // If getting product details failed
                          echo "&lt;p&gt;No product details found.&lt;/p&gt;"; // Display error message
                      }
                  }
                  ?&gt;
  &lt;form id="updateForm" name="updateForm" action="&lt;?php echo "?mode=update&amp;ID=" . $productDetails["ID"]; ?&gt;" method="post"&gt;                                
                      &lt;div&gt;
                          &lt;label for="updateFormProductCostPrice"&gt;ID&lt;/label&gt;
                          &lt;input id="updateFormProductCostPrice" name="ID" type="text" readonly 
                                 value="&lt;?php echo $productDetails["ID"]; ?&gt;"&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;label for="updateFormProductName"&gt;Film Name&lt;/label&gt;
                          &lt;input id="updateFormProductName" name="FilmName" type="text" value="&lt;?php echo $productDetails["FilmName"]; ?&gt;"&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;label for="updateFormProductDescription"&gt;Producer&lt;/label&gt;
                          &lt;textarea rows="4" cols="50" id="Producer" 
                                    name="productDescription"&gt;&lt;?php echo $productDetails["Producer"]; ?&gt;&lt;/textarea&gt;                
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;label for="updateFormProductPrice"&gt;Year Produced&lt;/label&gt;
                          &lt;input id="updateFormProductPrice" name="YearProduced" type="text" 
                                 value="&lt;?php echo $productDetails["YearProduced"]; ?&gt;"&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;label for="updateFormProductStock"&gt;Stock:&lt;/label&gt;
                          &lt;input id="updateFormProductStock" name="Stock" type="text" 
                                 value="&lt;?php echo $productDetails["Stock"]; ?&gt;"&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;label for="updateFormProductEan"&gt;Price:(&amp;#163)&lt;/label&gt;
                          &lt;input id="updateFormProductEan" name="Price" type="text" 
                                 value="&lt;?php echo $productDetails["Price"]; ?&gt;"&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;input id="updateSubmit" name="updateSubmit" value="Update product" type="submit"&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Then there is the php code to update the record (edit.php continued):</p>
  
  <pre><code>if (((!empty($_GET["mode"])) &amp;&amp; (!empty($_GET["id"]))) &amp;&amp; ($_GET["mode"] == "update")) { // If update
          echo "&lt;h1&gt;Update product&lt;/h1&gt;";
          if (isset($_POST["updateSubmit"])) { // If update form submitted
              // Check all parts of the form have a value
              if ((!empty($_POST["ID"])) &amp;&amp; (!empty($_POST["FilmName"])) 
                      &amp;&amp; (!empty($_POST["Producer"])) &amp;&amp; (!empty($_POST["YearProduced"])) 
                      &amp;&amp; (!empty($_POST["Stock"])) &amp;&amp; (!empty($_POST["Price"]))) {
                  // Create and run update query to update product details
                  $query = "UPDATE products "
                          . "SET FilmName = '" . $_POST["FilmName"] . "', "
                          . "Producer = '" . $_POST["Producer"] . "', "
                          . "YearProduced = '" . $_POST["YearProduced"] . "', "
                          . "Stock = " . $_POST["Stock"] . ", "
                          . "Price = '" . $_POST["Price"] . "' "
                          . "WHERE id=" . $_GET['ID'] . ";";
                  $result = mysqli_query($connection, $query);
  
                  if ($result == false) { // If query failed - Updating product details failed (the update statement failed)
                      // Show error message
                      echo "&lt;p&gt;Updating failed.&lt;/p&gt;";
                  } else{ // Updating product details was sucessful (the update statement worked)
                      // Show success message
                      echo "&lt;p&gt;Updated&lt;/p&gt;";
                          }
                                                                                  }
                                              }
  }
  </code></pre>
  
  <p>I do apologise that there is a lot of code here. Basically when I click edit in the table on the home page, I would expect it to load up the data for the respective row selected so I can update it.</p>
  
  <p>Currently, when I click the 'edit' link, it loads the edit page and it has the blank fields and says "getting product details failed". It would be great if it can retrieve the data for the respective row selected. Can someone help please? Thanks!</p>