Reputation: 67
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 log in the login_times
is set to 1 but when I do it again it stays the same.
Upvotes: 0
Views: 180
Reputation: 2229
The problem in your code is you didn't use $result
in the right way.
Besides, to prevent SQL injection you should use the prepare statement here is your example with prepare statement:
$email = "[email protected]";
$stmt = $conn->prepare("UPDATE usertable SET Login_Times= Login_Times + 1 where email=?");
$stmt->bind_param("s", $email);
$stmt->execute();
Upvotes: 2
Reputation: 67
$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!
Upvotes: 2
Reputation: 1272
You don't need to retrieve the login count before updating it, update it in one request like that :
$UpdateLastLogin = "UPDATE usertable SET login_times = login_times + 1 where email = '$email'";
mysqli_query($con, $UpdateLastLogin);
Upvotes: 1