Janaka Dombawela
Janaka Dombawela

Reputation: 1357

mysql update query doesn't work

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.

Upvotes: 1

Views: 3660

Answers (5)

RayanFar
RayanFar

Reputation: 587

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!

Upvotes: 3

Charaf JRA
Charaf JRA

Reputation: 8334

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");

Upvotes: 1

sri hs
sri hs

Reputation: 116

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");

Upvotes: 1

M I
M I

Reputation: 3682

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");

Upvotes: 1

David Hirst
David Hirst

Reputation: 2012

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.

Upvotes: 1

Related Questions