如何更新我的SQL表以显示上次登录日期?我试过“UPDATE表SET user_last_login =”'date(“m.d.y”)'“WHERE $ user_name”,但表没有更新。如果我单击表格列,它会将其更改为当前日期。
CREATE TABLE IF NOT EXISTS `users` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`user_name` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`user_pass` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`user_rank` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Member',
`user_ip` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`user_last_login` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
if(isset($_POST['login'])){
$name = $_POST['name'];
$password = $_POST['pass'];
$check_user = "select * from users where user_name='$name' AND user_pass='".md5($_POST['pass'])."'";
$run = mysql_query($check_user);
if(mysql_num_rows($run)>0){
//Create query
$qry="SELECT * FROM users WHERE user_name='$name' AND user_pass='".md5($_POST['pass'])."'";
$result=mysql_query($qry);
$update="UPDATE table SET user_last_login="'date("m.d.y")'"
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$users = mysql_fetch_assoc($result);
$_SESSION['id'] = $users['id'];
$_SESSION['name'] = $users['user_name'];
$_SESSION['pass'] = $users['user_pass'];
$_SESSION['rank'] = $users['user_rank'];
$_SESSION['ip'] = $users['user_ip'];
session_write_close();
}
}
}
答案 0 :(得分:1)
在session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
// $db is connection to database
$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);
$sql = "SELECT id FROM users WHERE username = '$myusername' and password = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
//session_register("myusername");
$_SESSION['login_user'] = $myusername;
header("location: account.php");
}else {
echo $error = "Your Login Name or Password is invalid";
}
$t_sql = "UPDATE users SET login_time=CURRENT_TIMESTAMP WHERE username = '$myusername'";
$t_result = mysqli_query($db,$t_sql);
}
答案 1 :(得分:0)
WHERE $user_name
应该是
WHERE user_name=$user_name
你当前的查询是我假设的错误。
答案 2 :(得分:0)
您需要插入MySql日期格式:
是2012-01-01 = YYYY-MM-DD
而不是m.d.y
答案 3 :(得分:0)
您的代码中存在一些错误。
首先,你把你的POST值放在一个变量中,你永远不会使用它!
$name = $_POST['name'];
$password = $_POST['pass'];
你可以做到
$name = $_POST['name'];
$password = md5($_POST['pass']);
$check_user = "select * from users where user_name='$name' AND user_pass='$password'";
为什么要使用下一行代码?
$qry="SELECT * FROM users WHERE user_name='$name' AND user_pass='".md5($_POST['pass'])."'";
并更新您的数据库。请尝试以下代码。
$date = date('Y-m-d');
$update="UPDATE table SET user_last_login='$date' WHERE user_name='$name' AND user_pass='$password'";"