title: PHP - How can I retrieve data from a database and display it in a form and make changes to it and update it?
body: <p>I'm new to PHP and I've been trying out PHP for a while. I have been able to retrieve data from my database and viewed it in a form but I cant seem to update it when I try to change any of the values. My code is below. Ive been trying a few things so sorry for the bad code </p>
  
  <pre><code>&lt;form action="user.php" method="POST"&gt;
                  &lt;h4&gt;Edit Account&lt;/h4&gt;
                  &lt;input type="text" name="editstudent_number" placeholder="Enter Student Number"&gt;
                  &lt;input type="submit" name="editaccount" value="Search"&gt;&lt;br&gt;
                  &lt;?php
                      if (isset($_POST['editaccount'])){
                          $GLOBALS['editstudent_number'] = $_POST['editstudent_number'];
                          $editstudent = $GLOBALS['editstudent_number'];
  
                          $edit_sql = "SELECT username, student_number, email, password, progress, rank FROM users WHERE student_number = '$editstudent'";
                          $edit_query = mysqli_query($conn, $edit_sql);
                          $edit_fetch = mysqli_fetch_assoc($edit_query);
  
                          $username = $edit_fetch['username'];
                          $student_number = $edit_fetch['student_number'];
                          $email = $edit_fetch['email'];
                          $password = $edit_fetch['password'];
                          $progress = $edit_fetch['progress'];
                          $rank = $edit_fetch['rank'];
  
                          echo "&lt;input type='text' name='username' value='$username' /&gt;&lt;br&gt;";
                          echo "&lt;input type='text' name='student_number' value='$student_number' /&gt;&lt;br&gt;";
                          echo "&lt;input type='text' name='email' value='$email' /&gt;&lt;br&gt;";
                          echo "&lt;input type='text' name='password' value='$password' /&gt;&lt;br&gt;";
                          echo "&lt;input type='text' name='progress' value='$progress' /&gt;&lt;br&gt;";
                          echo "&lt;input type='text' name='rank' value='$rank' /&gt;";
                          echo "&lt;input type='submit' name='editaccount' value='Save changes' /&gt;";                 
                      }
  
                          $GLOBALS['username'] = $_POST['username'];
                          $GLOBALS['student_number'] = $_POST['student_number'];
                          $GLOBALS['email'] = $_POST['email'];
                          $GLOBALS['password'] = $_POST['password'];
                          $GLOBALS['progress'] = $_POST['progress'];
                          $GLOBALS['rank'] = $_POST['rank'];
  
                          $edit_username = $GLOBALS['username'];
                          $edit_student_number = $GLOBALS['student_number'];
                          $edit_email = $GLOBALS['email'];
                          $edit_password = $GLOBALS['password'];
                          $edit_progress = $GLOBALS['progress'];
                          $edit_rank = $GLOBALS['rank'];
  
                      if (isset($_POST['editaccount'])){
                          $sql = "UPDATE users SET username='$edit_username', student_number='$edit_student_number', email='$edit_email', password='$edit_password', progress='$edit_progress', rank='$edit_rank' WHERE student_number = '$editstudent'";
                          $query = mysqli_query($conn, $sql);                     
                      }
                  ?&gt;
              &lt;/form&gt;
  </code></pre>