Title: Issue with login script
Tags: php;mysql;login-script
Question: I'm trying to make a login script but I'm stick with a problem:

```<?php

session_start();

if (isset($_POST['username'])) {
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $query = mysql_query(
        "SELECT id
         FROM users
         WHERE username = '$username'
         AND password = '$password'"
    );

    if (mysql_num_rows($query) == 0) {
        header('Location: ?error');
        exit();
    }

    // assign id to session
    $_SESSION['id'] = mysql_result($query, 0, 'id');
    mysql_query(
        "UPDATE users
         SET last_activity = ".time()."
         WHERE ".$_SESSION['id']
    );

    header("Location: /");
    exit();
}
?&gt;
```

The problem with this script is that it sets last_activity to current time on EVERY user.
Can't figure the problem out.

Some help would be greatly appricated, and yes I'm gonna look into password encrypting later :P

edit: found problem, should be ```mysql_query("UPDATE users SET last_activity = ".time()." WHERE id = ".$_SESSION['id']);```

Comment: Robert, put your solution as an answer and accept it


Here is another answer: You can use the mysql function now() as opposed to the php function time() - not that it makes a huge difference but its slightly neater as you don't have to break the string to do it. E.g. "Update tablename set time=now() where condition"


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2009:00:07/11/09', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><mysql><login-script>', 'Title': 'Issue with login script', 'ViewCount': 131, 'id': 1112962, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}