title: PHP: Update and REPLACE row in database
body: <p>I have a form that adds records to a database.  Each row has an 'Edit' button.  When a user clicks on the 'Edit' button, it takes the user to another page that has the form specifically for that id that was chosen to Edit, with the original information in it to Update.  The problem: When the Update button is clicked, the page adds a new record to the first page, instead of updating and replacing the information for the same id.
  Thanks for the help! :D</p>
  
  <p>Code Snippet of first page with list of records with 'Edit' option next to each:</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "database";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  
  $sql = "SELECT * FROM users ORDER BY lastname ASC";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = $result-&gt;fetch_assoc()) {
          echo "&lt;div class='container'&gt;";
              echo "&lt;table class='table'&gt;";
                  echo "&lt;tr id='hover'&gt;";
                      echo "&lt;td id='lastname'&gt;".$row["lastname"]."&lt;/td&gt;";
                      echo "&lt;td id='firstname'&gt;".$row["firstname"]."&lt;/td&gt;";
                      echo "&lt;td id='username'&gt;".$row["username"]."&lt;/td&gt;";
                      echo "&lt;td id='email'&gt;".$row["email"]."&lt;/td&gt;";
                      echo "&lt;td id='editdelete'&gt;
                      &lt;form action='Edit.php?id=" . $row['id'] . "' method='post'&gt;
                          &lt;input type='submit' name='edit' value='Edit' id='Edit'&gt;&lt;input type='hidden' name='id' value='" . $row['id'] . "'&gt;
                          &amp;nbsp;&lt;input type='submit' name='delete' value='Delete' id='delete'&gt;&lt;input type='hidden' name='id' value='" . $row['id'] . "'&gt;
                      &lt;/form&gt;&lt;/td&gt;";
                  echo "&lt;/tr&gt;";
              echo "&lt;/table&gt;";
          echo "&lt;/div&gt;";
      echo '&lt;br&gt;';
      }
  }
  else {
      echo "0 results";
  }
   $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>Code of 'Edit' page (second page):</p>
  
  <pre><code>&lt;?php
  // DB Connection Info
  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "database";
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
  die("Connection failed: " . $conn-&gt;connect_error);
  }
  // Code for EDIT button
  if (isset($_POST['edit'])) {
  $userid = $_POST['id'];
  }
  // Code for UPDATE button
  if (isset($_POST['update'])) {
  $userid = $_POST['id'];
  $firstname = $_POST['firstname'];
  $lastname = $_POST['lastname'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  $email = $_POST['email'];
  $sql = "UPDATE users SET firstname = '$firstname', lastname = '$lastname', username = '$username', password = '$password', email = '$email'  WHERE id = '$userid';";
  $conn-&gt;query($sql);
  }
  // SQL Query
  $sql = "SELECT * FROM users WHERE id = '$userid';";
  $result = $conn-&gt;query($sql);
  //Loop through and echo all the records
  while ($row = $result-&gt;fetch_assoc()){
  echo "&lt;form method='post' action='Index.php'&gt;";
  echo "&lt;input type='hidden' name='id' value='" . $row['id'] . "'&gt;";
  echo "First Name: &lt;input type='text' name='firstname' value='" .      $row['firstname'] . "'&gt; &lt;br&gt;";
  echo "Last Name: &lt;input type='text' name='lastname' value='" . $row['lastname'] . "'&gt; &lt;br&gt;";
  echo "Username: &lt;input type='text' name='username' value='" . $row['username'] . "'&gt; &lt;br&gt;";
  echo "Password: &lt;input type='password' name='password' value='" . $row['password'] . "'&gt; &lt;br&gt;";
  echo "Email: &lt;input type='text' name='email' value='" . $row['email'] . "'&gt; &lt;br&gt; ";
  echo "&lt;input type='submit' name='update' value='Update'&gt;";
  echo "&lt;/form&gt;";
  }
  $conn-&gt;close();
  ?&gt;
  </code></pre>