manoj kumar
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-con fig.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($quer yString)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-con fig.php");
session_start();
$username=$_POST['username '];
$password=$_POST['password '];
$userid=$_POST['userid'];
$username=stripcslashes($u sername);
$username=mysql_real_escap e_string($ username);
$username=htmlspecialchars ($username );
$password=stripcslashes($p assword);
$password=mysql_real_escap e_string($ password);
$password=htmlspecialchars ($password );
$sql="SELECT * FROM userregister WHERE username='$username' and password='$password'";
$result=mysql_query($sql)O R die("Error:".mysql_error() );
$row=mysql_num_rows($resul t);
$userinfo=mysql_fetch_asso c($result) ;
$_SESSION['userid']=$useri d;
$role=$userinfo['role'];
if($row==1){
$_SESSION['login_user']=$u sername;
$_SESSION['role']=$role;
$_SESSION['userid']=$id;
$_SESSION['username']=$use rname;
$_SESSION['intime']=$intim e;
$queryString = "INSERT INTO admin set userid='$userid' and intime='$NOW()' ";
$result1=mysql_query($quer yString)OR die("Error:".mysql_error() );
$row1=mysql_num_rows($resu lt1);
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";
}
?>
include_once("database-con
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($quer
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-con
session_start();
$username=$_POST['username
$password=$_POST['password
$userid=$_POST['userid'];
$username=stripcslashes($u
$username=mysql_real_escap
$username=htmlspecialchars
$password=stripcslashes($p
$password=mysql_real_escap
$password=htmlspecialchars
$sql="SELECT * FROM userregister WHERE username='$username' and password='$password'";
$result=mysql_query($sql)O
$row=mysql_num_rows($resul
$userinfo=mysql_fetch_asso
$_SESSION['userid']=$useri
$role=$userinfo['role'];
if($row==1){
$_SESSION['login_user']=$u
$_SESSION['role']=$role;
$_SESSION['userid']=$id;
$_SESSION['username']=$use
$_SESSION['intime']=$intim
$queryString = "INSERT INTO admin set userid='$userid' and intime='$NOW()' ";
$result1=mysql_query($quer
$row1=mysql_num_rows($resu
if($role=='admin'){
header('location:admin.php
}
if($role=='user'){
header('location:user.php'
}
if($role=='moderator'){
header('location:moderator
}
}else{
echo "No User Found by Given Information";
}
?>
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)O R die("Error:".mysql_error() );
$row=mysql_num_rows($resul t);
$userinfo=mysql_fetch_asso c($result) ;
$_SESSION['userid']=$useri d;
$role=$userinfo['role'];
if($row==1){
$_SESSION['login_user']=$u sername;
$_SESSION["userid"] = $row['id'];
$queryString = "INSERT INTO admin set userid='$row[id]' and intime='$NOW()' ";
$result1=mysql_query($quer yString)OR die("Error:".mysql_error() );
$row1=mysql_num_rows($resu lt1);
if($role=='admin'){
header('location:admin.php ');
}
if($role=='user'){
header('location:user.php' );
}
/////////////////unable to insert userid in mysql
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)O
$row=mysql_num_rows($resul
$userinfo=mysql_fetch_asso
$_SESSION['userid']=$useri
$role=$userinfo['role'];
if($row==1){
$_SESSION['login_user']=$u
$_SESSION["userid"] = $row['id'];
$queryString = "INSERT INTO admin set userid='$row[id]' and intime='$NOW()' ";
$result1=mysql_query($quer
$row1=mysql_num_rows($resu
if($role=='admin'){
header('location:admin.php
}
if($role=='user'){
header('location:user.php'
}
/////////////////unable to insert userid in mysql
ASKER
help me plzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz zzzzzzz
What are you expecting from this
Why are you not using a standard INSERT
$queryString = "INSERT INTO admin set userid='$row[id]' and intime='$NOW()' ";
Why are you not using a standard INSERT
$queryString = "INSERT INTO admin (userid, intime) VALUES ('{$row[id]}', NOW())";
$NOW() is not valid. SQL function is NOW()
ASKER
Sir,
but userid always inseted in admin table null,,,,whats wrong is there in my code,,,help me plzzzzzzzzzz
Regards
Manoj Kumar
but userid always inseted in admin table null,,,,whats wrong is there in my code,,,help me plzzzzzzzzzz
Regards
Manoj Kumar
ASKER
anyone can help me .plzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz zzzzzzzzzz zzzzzzzzzz zzzzz
Please see my last post.
but userid always inseted in admin table null,,,,whats wrong is there in my code,,,help me plzzzzzzzzzzI 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())";
ASKER
i am trying so maytime but same issue is there,,,,plz help me
ASKER
it's my logout.php
<?php
include_once("database-con fig.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($quer yString)OR die("Error:".mysql_error() );
echo "$queryString";
if(session_destroy())
{
header('Location: index.php');
}
?>
<?php
include_once("database-con
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($quer
echo "$queryString";
if(session_destroy())
{
header('Location: index.php');
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
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.
ASKER
beat solution
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.