我试图将time_out时间更新为捕获用户time_in的最后一个id,我的以下代码给了我这个错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
这是我的logout.php
session_start();
if(!isset($_SESSION['MEMBER_ID']) || (trim($_SESSION['MEMBER_ID']) == '')) {
header("location: index.php");
exit();
}
date_default_timezone_set('America/New_York');
$dt = date("Y-m-d h:i:s");
$id = $_SESSION['MEMBER_ID'];
$sql = "UPDATE hours SET time_out = '".$dt."' WHERE member_id IN (SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."'";
mysql_query($sql) or die(mysql_error());
//Unset the variables stored in session
unset($_SESSION['MEMBER_ID']);
unset($_SESSION['LOGIN_NAME']);
unset($_SESSION['PASS']);
这是架构
`hours_id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL,
`member_name` varchar(32) NOT NULL,
`team` varchar(32) NOT NULL,
`time_in` datetime NOT NULL,
`time_out` datetime DEFAULT NULL,
`dated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
unset($_SESSION['TEAM']);
在这个查询中有什么可能是错的。?
答案 0 :(得分:1)
是否已关闭查询中的圆括号?
$sql = "UPDATE hours SET time_out = '".$dt."' WHERE member_id IN (SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."')";
注意:停止使用已弃用的mysql_ *版本。有更好的选择,如mysqli或pdo
修改强>
使用单一查询,您可以解决此问题
$sql = "UPDATE hours SET time_out = '".$dt."' WHERE member_id IN (SELECT MAX(hours_id) FROM (SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."') AS t)";
让我知道它是否适合你
答案 1 :(得分:0)
在这里,您的解决方案忘了为内部查询完成括号。
$sql = "UPDATE hours SET time_out = '".$dt."' WHERE member_id IN (SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."')";
尝试上面的代码它会起作用。 建议停止使用旧函数开始使用MySQLi。Reference for MySQLi
答案 2 :(得分:0)
这应该是你的 sql
$sql="UPDATE hours SET time_out = '".$dt."' WHERE member_id IN (SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."')";
答案 3 :(得分:0)
使用此查询并再试一次,我希望它能够正常运行
$sql = "UPDATE hours SET time_out = '$dt' WHERE member_id IN (SELECT MAX(hours_id) FROM hours WHERE member_id = '$id'";

答案 4 :(得分:0)
您的代码应该是这样的
session_start();
if(!isset($_SESSION['MEMBER_ID']) || (trim($_SESSION['MEMBER_ID']) == '')) {
header("location: index.php");
exit();
}
date_default_timezone_set('America/New_York');
$dt = date("Y-m-d h:i:s");
$id = $_SESSION['MEMBER_ID'];
$sql = "UPDATE hours SET time_out = '".$dt."' WHERE member_id IN (SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."')";
mysql_query($sql) or die(mysql_error());
//Unset the variables stored in session
unset($_SESSION['MEMBER_ID']);
unset($_SESSION['LOGIN_NAME']);
unset($_SESSION['PASS']);