title: How to allow only one user edit the form in php?
body: <p>I have a php code as shown below in which <strong>usernames/passwords</strong> are verified through db and <strong>only one user are allowed to login (At Line X in the php code below)</strong>. </p>
  
  <p>In the php code, the table <strong>trace_users</strong> (<strong>user_name</strong>, <strong>open</strong>, <strong>write</strong> are the columns) keeps the track of logged in users. When any user is logged in, the column <strong>open</strong> is set to true with value <strong>1</strong> on <strong>write</strong> column. If the user logs out, the column <strong>open</strong> is set to false with value <strong>0</strong> on <strong>write</strong> column.   </p>
  
  <p><strong>logged in user</strong> => <code>open = true, write = 1</code> <br>
  <strong>when user logs out</strong> => <code>open = false, write = 0</code></p>
  
  <p><strong>php code:</strong></p>
  
  <pre><code>if ($user_from_db &amp;&amp; password_verify($password, $user_from_db-&gt;user_pass)) {
  
      $sql = "SELECT * FROM trace_users where open='true'";
      if($result1 = mysqli_query($connect, $sql)){
          if(mysqli_num_rows($result1) &gt; 0){   // Line X
              while($row = mysqli_fetch_array($result1)){
                  if($row['open'] == "true") {
                          if(!isset($_SESSION['admin'])) {
                              $message = "user " . $row['user_name'] . " is currently editing the form. Want to take over ?";
                              echo "&lt;script type='text/javascript'&gt;if(confirm('$message')) {   } else {  };&lt;/script&gt;";   // Line A                        
                              }
                      break;
                  }
              }
          } else{
              $_SESSION['admin'] = true;
              $_SESSION['user_name'] = $username;
              $open="true";
              $write="1";
              $stmt= $connect-&gt;prepare("UPDATE trace_users SET open=?, write=? WHERE user_name=?");
              $stmt-&gt;bind_param('sss', $open, $write, $_SESSION['user_name']);
              $stmt-&gt;execute();
          }
      }
  
  } else {
      echo "Invalid Username or Password";
  }
  </code></pre>
  
  <p>At line A if <strong>userA</strong> is logged in and another user lets say <strong>userB</strong> tries to login then it will say <code>userA is currently editing the form. Want to take over ?</code>. <br><br>
  What I want to achieve is if <strong>userB</strong> takes over the form <strong>on clicking ok in the alert box</strong> then <strong>userA</strong> should come in the readonly mode (revoke the write access) and <strong>userB</strong> should 
  be given full access (write). </p>
  
  <p><strong>userA</strong> => <code>open = true, write = 0</code> <br>
  <strong>userB</strong> => <code>open = true, write = 1</code></p>
  
  <p><strong>This is what I have tried:</strong></p>
  
  <p>After Line A, I am thinking to add these lines so that the user who is trying to take over should be able to login.  </p>
  
  <pre><code>$_SESSION['admin'] = true;
  $_SESSION['user_name'] = $username;
  $open="true";
  $write="1";
  $stmt= $connect-&gt;prepare("UPDATE trace_users SET open=?, write=? WHERE user_name=?");
  $stmt-&gt;bind_param('sss', $open, $write, $_SESSION['user_name']);
  $stmt-&gt;execute();
  </code></pre>
  
  <p><strong>Problem Statement:</strong></p>
  
  <p>I am wondering what changes I should make in the php code above so that if <strong>userA</strong> is logged in and <strong>userB also want to login and try to take over the form</strong> 
  then there should be no write access for <strong>userA</strong>, and <strong>userB</strong> should be given full access i.e. </p>
  
  <p>userA <code>(open=true , write=0)</code></p>
  
  <p>userB <code>(open=true, write=1)</code></p>
  
  <p><strong>Note:</strong> For no write access, the save button will not appear for the user. </p>