title: What should I put in my updating.php when I need to update users status if online or offline every 5 seconds?
body: <p>Hi I've made a messaging feature,it displays the receivers of the message in an account, beside the receiver's name is a green or red dot, for online or offline, it is successful when I log in 2 accounts and messaged each other, when I open the first account, the other account is offline, when I open the second account, the other is online, what my problem is I want to update their status in every five seconds so that their dot will be accurate whenever they log out or log in. I have made a javascript and ajax below this page left-col.php and I've tried copying the whole code, putting it to updating.php but it did not work, what should be the appropriate data inorder to update user receiver status in every 5 seconds inside the updating.php? and also, does my the javascript and ajax needs a library to be included that's why it didn't work? Please help.</p>
  
  <pre><code>&lt;?php 
  require('connection.php');
  $user_name=$_SESSION['username'];
  $user_id=$_SESSION['user_id'];
  if(!isset($_SESSION['user_id'])){
      header("LOCATION: index.php");
  }else{
      $advance_time=time()+15;
      $query=mysqli_query($con,"SELECT * FROM status WHERE user_id='$user_id'");
      if(mysqli_num_rows($query)&gt;0){
          mysqli_query($con,"UPDATE status SET status='$advance_time' WHERE user_id='$user_id'");
      }else{
          mysqli_query($con,"INSERT INTO status(user_id,status) VALUES ('$user_id','$advance_time')");
      }
  }
  ?&gt;
  &lt;div id="left-col-container"&gt;
  &lt;div style="cursor:pointer" onclick="document.getElementById('new-message').style.display='block'" class="white-back"&gt;
  &lt;p align="center"&gt;New Message &lt;/p&gt;
  &lt;/div&gt;
  
  &lt;?php 
  
  $q='SELECT DISTINCT `receiver_name`,`sender_name`,`date_time`
  FROM `messages` WHERE 
  `sender_name`="'.$_SESSION['username'].'" OR
  `receiver_name`="'.$_SESSION['username'].'"
  ORDER BY `date_time` DESC';
  $e='SELECT * from messages';
  $r=mysqli_query($con,$q);
  
  
  if($r){
      if(mysqli_num_rows($r)&gt;0){
          $counter=0;
          $added_user=array();
          while($row=mysqli_fetch_assoc($r)){
              $sender_name=$row['sender_name'];
              $receiver_name=$row['receiver_name'];
              $timestamp=$row['date_time'];
              if($_SESSION['username']==$sender_name){
                  //add the receiver_name but only once
                  //so to do that check the user in array
                  if(in_array($receiver_name,$added_user)){
                      //dont add receiver_name because
                      //he is already added
                  }else{
                      //add the receiver_name
                      ?&gt;
                      &lt;div class="grey-back"&gt;
                      &lt;img src="images/s.jpg" class="image"/&gt;
                      &lt;?php 
                      echo '&lt;a href="?user='.$receiver_name.'"&gt;'.$receiver_name.'&lt;/a&gt;';
                     $fetch_content=mysqli_query($con,"SELECT * FROM users JOIN status ON `users`.`id`=`status`.`user_id`");
                      while($row_fetch=mysqli_fetch_array($fetch_content)){
                          $time=$row_fetch[5];
                          if($time&lt;= time()){
                              $status = "&lt;img src='images/r.png' height='10' width='10' style='float:right'&gt;";
                          }else{
                              $status= "&lt;img src='images/a.png' height='10' width='10' style='float:right'&gt;";
                          }
                      }
                       echo $status;
                      ?&gt;
                      &lt;/div&gt;
                      &lt;?php
                      //as receiver_name added so
                      ///add it to the array as well
                      $added_user=array($counter=&gt;$receiver_name);
                      //increment the counter
                      $counter++;
                  }
              }elseif($_SESSION['username']==$receiver_name){
                  //add the sender_name but only once
                  //so to do that check the user in array
                  if(in_array($sender_name,$added_user)){
                      //dont add sender_name because
                      //he is already added
                  }else{
                      //add the sender_name
                      ?&gt;
                      &lt;div class="grey-back"&gt;
                      &lt;img src="images/s.jpg" class="image"/&gt;
                      &lt;?php echo '&lt;a href="?user='.$sender_name.'"&gt;'.$sender_name.'&lt;/a&gt;';
                        $fetch_content=mysqli_query($con,"SELECT * FROM users JOIN status ON `users`.`id`=`status`.`user_id`");
                      while($row_fetch=mysqli_fetch_array($fetch_content)){
                          $time=$row_fetch[5];
                          if($time&lt;= time()){
                              $status = "&lt;img src='images/r.png' height='10' width='10' style='float:right'&gt;";
                          }else{
                              $status= "&lt;img src='images/a.png' height='10' width='10' style='float:right'&gt;";
                          }
                      }
                       echo $status;
                      ?&gt;
                      &lt;/div&gt;
                      &lt;?php
                      //as sender_name added so
                      ///add it to the array as well
                      $added_user=array($counter=&gt;$sender_name);
                      //increment the counter
                      $counter++;
                  }
              }
              }
          }
          else{
          //no message sent
          echo 'no user';
          }
      }else{
      //query problem
      echo $q;
  }
  ?&gt;
  
  
  
  &lt;!-- end of left-col-container --&gt;
  &lt;/div&gt;
  &lt;input type="hidden" value="&lt;?php echo $user_id; ?&gt;" id="from_user_id"&gt;
  &lt;script type="text/javascript"&gt;
  setInterval(function(){updating_status()},5000);
  function updating_status(){
      let this_user = $('#from_user_id').val(); 
      $.ajax({
          method: "POST",
          url: "updating.php",
          data: {from_user:this_user},
      success: function(response){
          $('#content').html(response);
      }
      });
  }
  &lt;/script&gt;
  </code></pre>