I have been out of the PHP loop for a while and when I returned, yesterday, I quickly found that many old php functions to interact with mysql are being phased out. This is fine, but I am really struggling with the new pdo code. Lets say a user is going to log in, I want to see if his username/password combo is correct. Then I want to update his login count, move the date/time of his last login to 'previouslogin' and update 'lastlogin' with the current date/time. Problems I am having. I can't get the logincount to increment, not even the variable, I can't assign the lastlogin to the variable, which prevents me being able to update previouslogin. Can you show me what I am doing wrong?
public function accountLogin()
{
$sql = "SELECT UserID, UserName, PositionID, LoginCount, LastLogin
FROM users
WHERE UserName=:user
AND Password=:pass
LIMIT 1";
try
{
$stmt = $this->_db->prepare($sql);
$stmt->bindParam(':user', $_POST['username'], PDO::PARAM_STR);
$stmt->bindParam(':pass', $_POST['password'], PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount()==1)
{
$_SESSION['Username'] = htmlentities($_POST['username'], ENT_QUOTES);
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserID']= $stmt->fetch()[0];
$_SESSION['PositionID']= $stmt->fetch()[2];
$logincount= $stmt->fetch()[3];
$lastlogin= $stmt->fetch()[4];
$sql = "UPDATE users SET LoginCount = ". $logincount + 1 . ",
PreviousLogin = " . $lastlogin ."
WHERE UserID = " . $_SESSION['UserID'];
$stmt = $this->_db->exec($sql);
return TRUE;