简体   繁体   中英

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.

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!

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.

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

将查询更改为以下格式,然后尝试,

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

只需从查询中的ID中删除单引号 ,因为它是整数而不是String

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM