如何向用户显示上次访问

时间:2015-06-27 21:14:17

标签: php mysql database

我想显示用户上次访问的时间,我用一些互联网帮助制作了这个脚本,但当然它不起作用。

$sql="update users set last_visit = NOW() where id = ".$_SESSION['userid'].
if($sql === false)
{
echo 'Query mislukte bij regel '. __LINE__ . '<br>';
}

有人知道我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

正如您所要求的示例,请检查以下代码: -

 <?php
      session_start(); // start session to get SESSION variable value
      error_reporting(E_ALL); // catch all type of errors
      ini_set('display_errors',1); // display those errors

      $conn = mysqli_connect('servername','username','password','databasename'); // fill your database details here

      $user_id = mysqli_real_escape_string($conn,$_SESSION['userid']);// prevent form SQL injection
      $last_visit = date('Y-m-d H:i:s'); // you can change date format according to your wish

      $sql = "UPDATE users SET last_visit = '". $last_visit ."' WHERE id = '". $user_id ."'"; // change in query

      $result = mysqli_query($conn,$sql) or die(mysqli_error($conn)); // execute query and catch error if any happen

      if($result){
          echo "Record updated successfully";
      }else{
         echo 'Some error occur while updating. Please try again.';
      }
?>

注意: - 只需输入您的数据库凭据并继续使用。