Search code examples
phpdatabasemysqli

Login times counter in php


I have a database and i am trying to make login times count using php this is my code :

$GetLoginTimes = "Select Login_Times from usertable where email = '$email'";
$GetLoginTimes2 = mysqli_fetch_array($GetLoginTimes);
$LoginTimesEdited = $GetLoginTimes2 + 1;
$UpdateLastLogin = "UPDATE usertable SET Login_Times = '$LoginTimesEdited' WHERE email = '$email'";
mysqli_query($con, $UpdateLastLogin);

when i login the login_times is set to 1 but when i do it again it stays the same TIA!


Solution

  • $Update = "UPDATE Users SET Last_Login = ?, Login_Times = Login_Times + 1 WHERE Users.Email = ?";
    $stmt = $db->execute_query($Update, [$Date, $Email]);
    

    This is the best way to achieve it i believe as it combines both queries so more efficient and is safe against SQL injections because it uses parameters and most importantly works fine!