Carlos Santiago
Carlos Santiago

Reputation: 73

Creating log records when updating users records

I'm trying to create a log table that register all the events the users do on my app (create and update records).
So far I can register on the log table the a user who creates a record, but now I need to create a new register for the log table when the user update something, eg: The user Mike register a deviler of a Phone to the employee Kyle (This is working so far) but then the user Mike register that the employee Kyle returned that Phone. (the log should now register the update but adding a new record on log table.
Thanks in advance!
I have this so far:

$pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO ativos (ativo,comentario,data_aquisicao,localizacao,fabricante,modelo,imei,
            numero_serie,ativo_sap,evento,data_evento,id_colaborador) 
            SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, id_colaborador 
            FROM colaboradores 
            WHERE nome = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($ativo,$comentario,$data_aquisicao,$localizacao,$fabricante,$modelo,$imei,$numero_serie,$ativo_sap,$evento,$data_evento,$id_colaborador));

if ($q) {
    //get last ID that was generated by previos insert
    $id_ativo = $pdo->lastInsertId();
    }
    $log  = "INSERT INTO log_ativos (acao_log,data_log,id_ativo,id_colaborador)
SELECT ?,?,?, id_colaborador
FROM colaboradores
WHERE nome = ?";
    $qlog = $pdo->prepare($log);
    $qlog->execute(array($acao_log2,$data_log2,$id_ativo,$id_colaborador));
    Database::disconnect();

And it's not creating a new record on log table when I update the main record of ativos table

Upvotes: 1

Views: 46

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

I think it's due to trying to find a record in colaboradores with nome equal to $id_colaborador, if this last value is the actual ID, then insert it directly...

$log  = "INSERT INTO log_ativos (acao_log,data_log,id_ativo,id_colaborador)
            VALUES(?,?,?,?)";
$qlog = $pdo->prepare($log);
$qlog->execute(array($acao_log2,$data_log2,$id_ativo,$id_colaborador));

Upvotes: 1

Related Questions