I have developed a login form with database table that holds the user details. I would like to have an additional table that holds the date of the user login.
The table users contain:
users( id , username, password)
the table user_login has:
user_login(id, user_id, login_date)
here user_id is the foreign key for the id from users table and the type for login_date is set to timestamp.
I have tried something like this:
$db = mysqli_connect('localhost', 'root', '', 'registration');
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password_1']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT *
FROM users
WHERE username='$username'
AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$name=$_SESSION['username'];
$date = date('Y-m-d H:i:s');
$indate= "INSERT INTO user_login
(user_id, login_date)
SELECT *
FROM users as u
WHERE username='$name'
AND login_date= '$date'";
mysqli_query($db, $indate);
// $_SESSION['success'] = "You are now logged in";
header('location: profile.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
but when i execute the above code, the user can login successfully but the date is not added to the table, I don't know where i went wrong. Can someone tell me the correct query to insert the date in the table user_login