Record record in table at each user access

Asked

Viewed 1,026 times

-1

I need to create a script that records a last-accessed log in a table, but my Insert is not working. I created an Access class, where it logs in.

If anyone can help me. Thank you.

My class of access:

class Acesso {

    public function run() {

        $html = '';

        Session::setValue('erro', '');

        switch (App::getAction()) {
            case 'logout':
                $this->logout();
                break;

            case 'validar':
                $this->validar();
                break;

            default:
                $html = $this->login();
                break;
        }

        return $html;
    }

    public function login() {

        $html_login = new Html();
        $html = $html_login->load('view/acesso.html');


        return $html;
    }

    public function validar() {

        $dados = Connection::select("SELECT login,senha,nome,departamento_id FROM users WHERE login='" . $_POST['usuario'] . "'");
        Connection::close();

        foreach ($dados as $reg):
            if ($_POST['senha'] == $reg['senha']):
                Session::setValue('logado', true);
                Session::setValue('departamento', $reg['departamento_id']);
                Session::setValue('nome', $reg['nome']);

            else:
                echo "<script>alert('Dados inválidos!'); location.href= 'index.php';</script>";
            endif;
        endforeach;

        $sql = "update log set user_id= user_id, user_data= user_data where id=" . App::$key;
                    $dados = Connection::exec($sql);

        header('Location: ' . URL);
    }

    public function logout() {

        Session::setValue('logado', false);
        header('Location: ' . URL);
    }

}

When I run, you present this error below:

Fatal error: Call to a member function exec() on a non-object in
  • I don’t know if I understood you want to count how many times the user has logged in? or make a log table each time he logs in you save the date and time?

  • How many times has the user logged in.

  • Make a select to pick the value, saved in a variable and makes $variable +1

  • I don’t know how to do it, you can give me an example?

  • Try to be more specific, which isn’t working?

  • It does not save the record to the table, and displays this Fatal error: Call to a Member Function exec() on a non-object in

  • $sql = "update users set acesso= acesso + 1 Where login = '" . $_POST['user'] . "'"; $data2 = Connection::exec($sql);

Show 2 more comments

2 answers

1

Friend, for this particular case, first I suggest you create a table specifically for this. Every time you perform an Update, database paging can get a bit "disorganized" and depending on the size of your system, this can be a problem.

However, if you want to keep it as it is, you need to search for the record in the database, run "+1" in PHP and then run Update by passing the changed object. It’s just to get an idea, but the example would be like this:

$count = $obj->TotalAcessos +1;
$sql = "update 'users' set 'acesso'= '" . $count . "'where id=" . App::$key;
$dados = Connection::exec($sql);

If you want to follow my first suggestion, the table can contain Id (autoincrement), Idusuario and Login Date. Every time someone logs in, you enter this table. Then you can identify how many times he has logged in, for example:

SELECT COUNT(Id) FROM HistoricoLogin WHERE IdUsuario = 1

In addition to the fact that, as you have saved dates, you can know the amount of accesses of a specific day or a range of dates.

  • 1

    It didn’t work, but I think it’s best to create a log table. I’ll try to develop something.

0

In this passage

$sql = "update `users` set `acesso`= acesso + 1 where id=" . App::$key;

Trade for this:

$sql = "update `users` set `acesso`= `acesso` + 1 where `login` = '" . $_POST['usuario'] . "'";
  • It didn’t work tbm :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.