0

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;
2
  • 1
    Your code will only work with php 5.4 what do you have installed? Commented Jan 12, 2013 at 21:32
  • php 5.4 installed. It works halfway, but I can not set PreviousLogin to lastlogin, and my mysql table is not updating lastlogin to current timestamp, even though login count is incrementing. Commented Jan 14, 2013 at 15:50

1 Answer 1

1

To increment a MySQL field by one, you can do LoginCount = LoginCount + 1

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)
    {
        $row = $stmt->fetch( PDO::FETCH_OB );
        $_SESSION['Username'] = htmlentities($row->UserName, ENT_QUOTES, "UTF-8");
        $_SESSION['LoggedIn'] = 1;
        $_SESSION['UserID']= $row->UserID;
        $_SESSION['PositionID']= $row->PositionID;                                
        $lastlogin= $row->LastLogin;
        $sql = "UPDATE users SET LoginCount = LoginCount + 1, 
        PreviousLogin = " . $lastlogin ."
        WHERE UserID = " . $_SESSION['UserID'];

        $stmt = $this->_db->exec($sql);

        return TRUE;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.