从MySQL选择AJAX

Hello need help basically when a user log on it will put the time and date in the Seen column on MySQL, so when a user log off or close the browser it should show user is offline and when they on it should say user is online.

<?
$username = $_SESSION['membersusername'];
if(isset($username)){
 mysql_query("UPDATE users SET seen=NOW() WHERE username='$username'");
}
?>

And the I think AJAX?

<?
$sql=mysql_query("SELECT seen FROM users");
while($rows=mysql_fetch_array($sql)){
 $seen=$rows['seen'];
 if ( $seen < date("Y-m-d H:i:s",strtotime('-20 seconds', time()))){
  echo "User Is offline";
 }else{
  echo "User is online";
 }
}
?>

When I do the above code it does this... http://prntscr.com/2sd7fb

Can anyone help?

in ajax code put below code.I think it would be helpful.

<?
$sql=mysql_query("SELECT seen FROM users WHERE username='$username'");
while($rows=mysql_fetch_array($sql)){
$seen=$rows['seen'];
if ( $seen < date("Y-m-d H:i:s",strtotime('-20 seconds', time()))){
echo "User Is offline";
}else{
echo "User is online";
}
}
?>

For doing AJAX you need Javascript, there are many useful resources around the web. It is common to use a ready cross-browser implementation, such as JQuery instead of implementing it oneself since it is a daunting task. But for starters, you could try out a simple demo and learn from it: W3Schools ajax try-it

P.S. The SQL query you're using is vulnerable to something called SQL-injection and a malicious person could exploit it to cause serious harm to your DB. Please see: mysqli and learn about prepared statements.