1

I have this php file for store the logout time. Using insert query I store the login time first. Then I store the logout time using this logout script. The error I'm getting is the one I mentioned in my code which is failed to insert logout time. the $lgotime and $lrec are echoing correctly. Below is my code:

<?php

session_start();

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

$lrec = $_SESSION['lreclast'];

echo $lgotime;

echo "</br>";

echo $lrec;

mysql_query("UPDATE tbl_login_record SET logoff_time='$lgotime' WHERE id = '$lrec'") or die("failed to insert logout time");

session_destroy();
header("Location:../login.php");
?>

the $lrec variable is coming from mysql_insert_id() from the script that has the insert query. Help will be really appreciated.

3
  • what.. you are trying to insert or update? Commented Mar 17, 2014 at 10:03
  • I'm trying to update. Commented Mar 17, 2014 at 10:04
  • 2
    hey where is the connection string here... Commented Mar 17, 2014 at 10:20

5 Answers 5

3

if your mysql query dosent work you can change your code like this:

mysql_query("UPDATE tbl_login_record SET logoff_time='".$lgotime."' WHERE id = '".$lrec."'") or die("failed to insert logout time");

it always works for me!

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that your query is not sending the data, its sending strings

mysql_query("UPDATE tbl_login_record SET logoff_time='$lgotime' WHERE id = '$lrec'") or die("failed to insert logout time");

Should be,

mysql_query("UPDATE tbl_login_record SET logoff_time='{$lgotime}' WHERE id = {$lrec}") or die("failed to insert logout time");

To be honest I would not suggest this either and would use parametrized queries instead.

3 Comments

you are in php , not java or javascript ; concat using dot
Even so, the ID is going to be an integer presumably so id='$lrec' will still be incorrect.
@DavidHirst: MySQL ignores quotes around numerics when being set to numeric fields.
1

Please give it a try. You are not using session_start() on top of this page and change query as

<?php 
 session_start();
 $lgotime = date("Y-m-d H:i:s");

$lrec = $_SESSION['lreclast'];

echo $lgotime;

echo "</br>";

echo $lrec;

mysql_query("UPDATE tbl_login_record SET logoff_time='".$lgotime."' WHERE id = '".$lrec."'") or die("failed to insert logout time");

session_destroy();
header("Location:../login.php");

Comments

1

Change the query into the below format and try,

mysql_query("UPDATE tbl_login_record SET logoff_time='".$lgotime."' WHERE id = '".$lrec."'") or die("failed to insert logout time");

Comments

1

Just remove single quotes from the id in your query because it is integer not String

mysql_query("UPDATE tbl_login_record SET logoff_time='$lgotime' WHERE id = $lrec") or die("failed to insert logout time");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.