Record record in table with each user access

-1

I need to create a script that records a last-access 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 access class:

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, it displays this error below:

Fatal error: Call to a member function exec() on a non-object in
    
asked by anonymous 16.02.2016 / 20:18

2 answers

1

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

However, if you want to keep as is, you need to search the database, run the "+1" in PHP and then run the Update passing the changed object. It's just to have 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 since you have saved dates, you can know the number of hits for a specific day or a range of dates.

    
16.02.2016 / 21:17
0

In this section

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

Switch to this:

$sql = "update 'users' set 'acesso'= 'acesso' + 1 where 'login' = '" . $_POST['usuario'] . "'";
    
16.02.2016 / 22:17