简体   繁体   English

mysql更新查询不起作用

[英]mysql update query doesn't work

I have this php file for store the logout time. 我有这个php文件,用于存储注销时间。 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 . 我遇到的错误是我在代码中提到的错误,该错误failed to insert logout time the $lgotime and $lrec are echoing correctly. $lgotime$lrec正确回显。 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. $lrec变量来自具有插入查询的脚本的mysql_insert_id() Help will be really appreciated. 帮助将不胜感激。

if your mysql query dosent work you can change your code like this: 如果您的mysql查询工作正常,则可以这样更改代码:

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 您不在此页面顶部使用session_start()并将查询更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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