Link to home
Start Free TrialLog in
Avatar of manoj kumar
manoj kumarFlag for India

asked on

Record user login and logout timing In php,,i am getting login and logout time, but in database userid alway show 0,

<?php
include_once("database-config.php");
      session_start();
      

      $strDateNew=date("d-m-Y H:i:s");
       if ($_SESSION['userid']){

      $queryString = "update admin set outtime=NOW()  WHERE  userid = '$_SESSION[userid]' and intime='$strDateNew' ";
        $result1=mysql_query($queryString)OR die("Error:".mysql_error());
        echo "$queryString";
        }
        if(session_destroy())
   {
    header('Location: index.php');
}
   
      
?>here  userid passes 0 to database ,i want userid so that in every month i dispay userlogin and logout time  so kindly help me
here is also my authentic.php
<?php ini_set('display_errors', '1'); ?>  
<?php
include_once("database-config.php");
 
  session_start();
 $username=$_POST['username'];
 $password=$_POST['password'];
 $userid=$_POST['userid'];
 $username=stripcslashes($username);
 $username=mysql_real_escape_string($username);
 $username=htmlspecialchars($username);
 
 $password=stripcslashes($password);
 $password=mysql_real_escape_string($password);
 $password=htmlspecialchars($password);
 $sql="SELECT * FROM userregister  WHERE username='$username' and password='$password'";
$result=mysql_query($sql)OR die("Error:".mysql_error());

 $row=mysql_num_rows($result);
$userinfo=mysql_fetch_assoc($result);
$_SESSION['userid']=$userid;
 $role=$userinfo['role'];
 
 if($row==1){

  $_SESSION['login_user']=$username;
  $_SESSION['role']=$role;
  $_SESSION['userid']=$id;
  $_SESSION['username']=$username;
  $_SESSION['intime']=$intime;
 
  $queryString = "INSERT INTO admin set userid='$userid' and intime='$NOW()' ";
  $result1=mysql_query($queryString)OR die("Error:".mysql_error());
 $row1=mysql_num_rows($result1);
  if($role=='admin'){
   
   
  header('location:admin.php');
  }
  if($role=='user'){
   
  header('location:user.php');
  }
  if($role=='moderator'){
  header('location:moderator.php');
  }
   
   
 }else{
  echo "No User Found by Given Information";
 }
 
 
?>
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I cannot tell from the above what you are asking - can you make it a bit more clear.

Also I see you are using the mysql library - please be aware this library is deprecated and removed in later versions of PHP consider moving to MySQLi or PDO.
Avatar of manoj kumar

ASKER

actually this is attendence ragistry for employee,every day user come and login and logout  and a superuser admin is there who can generate login and logout time for each user on daily bases,,,,so i am sucessfully store login and logout time but unable to insert userid:
My DataBase Struture:
Admin table have:
id:
userid:
logintime:
logouttime:
and usertable:
id:
username:
password:
role:
my issue is this code////////////////////////////////
$sql="SELECT * FROM userregister  WHERE username='$username' and password='$password'";
$result=mysql_query($sql)OR die("Error:".mysql_error());

$row=mysql_num_rows($result);
$userinfo=mysql_fetch_assoc($result);
$_SESSION['userid']=$userid;
 $role=$userinfo['role'];
 
 if($row==1){

     $_SESSION['login_user']=$username;
      $_SESSION["userid"] = $row['id'];
 
  $queryString = "INSERT INTO  admin  set userid='$row[id]' and intime='$NOW()' ";
  $result1=mysql_query($queryString)OR die("Error:".mysql_error());
 $row1=mysql_num_rows($result1);
  if($role=='admin'){
   
   
  header('location:admin.php');
  }
  if($role=='user'){
 header('location:user.php');
}
/////////////////unable to insert userid in mysql
help me plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
What are you expecting from this
 $queryString = "INSERT INTO  admin  set userid='$row[id]' and intime='$NOW()' ";

Open in new window


Why are you not using a standard INSERT
 $queryString = "INSERT INTO  admin (userid, intime) VALUES ('{$row[id]}', NOW())";

Open in new window

$NOW() is not valid. SQL function is NOW()
Sir,
but userid always inseted in admin table null,,,,whats wrong is there in my code,,,help me plzzzzzzzzzz

Regards
Manoj Kumar
anyone can help me .plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Please see my last post.
but userid always inseted in admin table null,,,,whats wrong is there in my code,,,help me plzzzzzzzzzz
I have already shown you what is wrong - your UPDATE statement is badly formed. That UPDATE statement results in (NULL) values being inserted into the database - you need to change it to the correct format as I demonstrated in my last post
 $queryString = "INSERT INTO  admin (userid, intime) VALUES ('{$row[id]}', NOW())";

Open in new window

i am trying so maytime but same issue is there,,,,plz help me
it's my logout.php

<?php
include_once("database-config.php");
      session_start();
      

      $strDateNew=date("d-m-Y H:i:s");
 
        
     
      

      $queryString = "update admin set outtime=NOW()  WHERE  userid = '$session[userid]' and intime='$strDateNew' ";
        $result1=mysql_query($queryString)OR die("Error:".mysql_error());
  echo "$queryString";
      
     if(session_destroy())
   {
    header('Location: index.php');
   }
      
?>
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sir i donot understand why t show 0 in my admin table,,,i am trying many times


sir can u give me suggestion why it inseted 0 to my admin table
Please see your other thread

https://www.experts-exchange.com/questions/29077918/userid-not-inserted.html?anchorAnswerId=42432304#a42432304

And please don't open duplicate questions - it creates too much confusion.
beat solution
sir how to delete My all converstion for dicussing this question and get solution,because when i seach in google it show This Link

Sir can u Remove This Link so No one can See: