0

I'm trying to create a log table for my DB that register all the actions that happen, and I was thinking of creating one log table for each table so I'll be able to read the information easily.
But I'm not sure how to do it because I want the input of the log table to a variable where I'll format the text and put the other variables to register the input.
Eg: $log = '[' . $data_log . '] ' . ' The item '. $ativo .' with the S/N '. $numero_serie .' was delivered to the employee '. $id_colaborador);

And for the DB I have this, I know it's wrong but I don't know why..

if ($valid) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $acao_log2 = '[' . $data_log . '] ' . ' O ativo '. $ativo .' com o S/N '. $numero_serie .' foi entregue ao colaborador '. $id_colaborador . ' [' . $user . ']' ; 



    $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 = ?";



    $log = "INSERT INTO log_ativos (acao_log, data_log, id_ativo)
            VALUES (". $acao_log2 . "," . $data_log . ", id_ativo = id_ativo WHERE id_ativo = ?";

    $q = $pdo->prepare($sql);
    $qlog = $pdo->prepare($log);
    $q->execute(array($ativo,$comentario,$data_aquisicao,$localizacao,$fabricante,$modelo,$imei,$numero_serie,$ativo_sap,$evento,$data_evento,$id_colaborador));
    $qlog->execute(array($acao_log,$data_log,$id_ativo));
    Database::disconnect();


The thing is, I dont want the user to input nothing for the logs, it should be automatic :/

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\gestao\Ativos\create.php:129 Stack trace: #0 C:\xampp\htdocs\gestao\Ativos\create.php(129): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\gestao\Ativos\create.php on line 129

1 Answers1

2

Your queries are wrong in so many levels.

  1. SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, id_colaborador This really wrong Table and Column names cannot be replaced by parameters in PDO.

  2. Your Insert query is wrong also :

    $log = "INSERT INTO log_ativos (acao_log, data_log, id_ativo) VALUES (". $acao_log2 . "," . $data_log . ", id_ativo = id_ativo WHERE id_ativo = ?";

The above query is so wrong.. INSERT does not support where clause, instead you are looking at update.

This is what you might be looking for.

<?php
if ($valid) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $acao_log2 = '[' . $data_log . '] ' . ' O ativo ' . $ativo . ' com o S/N ' . $numero_serie . ' foi entregue ao colaborador ' . $id_colaborador . ' [' . $user . ']';

    $sql = "INSERT INTO ativos (ativo,comentario,data_aquisicao,localizacao,fabricante,modelo,imei,
            numero_serie,ativo_sap,evento,data_evento,id_colaborador) 
            SELECT column1,column2,column3,column4,column5,column6,column7,column8,column9,column10,column11, id_colaborador 
            FROM colaboradores 
            WHERE nome = ?";

    $q = $pdo->prepare($sql);
    $q->execute(array($nomeVariable));

    $log  = "UPDATE log_ativos SET acao_log = ? , data_log = ? WHERE id_ativo = ? ";
    $qlog = $pdo->prepare($log);
    $qlog->execute(array($id_ativo));

}

?>

Take sometime and do sql quires tutorials and be friend with php manual and sql manual

UPDATE

Based on your comment from below,

I want to create a new log record for that table based on the id_ativo that the 1st query will generate,

Then what you need to do when the 1st statement inserted successfully get the last insert id that was generate and use in the next insert.

This is how :

<?php

if ($valid) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $acao_log2 = '[' . $data_log . '] ' . ' O ativo ' . $ativo . ' com o S/N ' . $numero_serie . ' foi entregue ao colaborador ' . $id_colaborador . ' [' . $user . ']';

    $sql = "INSERT INTO ativos (ativo,comentario,data_aquisicao,localizacao,fabricante,modelo,imei,
            numero_serie,ativo_sap,evento,data_evento,id_colaborador) 
            SELECT column1,column2,column3,column4,column5,column6,column7,column8,column9,column10,column11, id_colaborador 
            FROM colaboradores 
            WHERE nome = ?";

    $q = $pdo->prepare($sql);
    $q->execute(array($nomeVariable));

    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) VALUES(?,?,?)";
    $qlog = $pdo->prepare($log);
    $qlog->execute(array($acao_log2,$data_log,$id_ativo));

}

?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • 1st I don't want to update the `log_ativos` I want to create a new `log record` for that table based on the `id_ativo` that the 1st query will generate, and when I'm saying that it works the way it is it's because I'm testing it many and many times and it does not give me any problem man... The only issue I'm having is with the `$log` query! – Carlos Santiago Dec 07 '18 at 12:50
  • You don't use a where clause on `INSERT..` use that in `UPDATE,DELETE,SELECT` – Masivuye Cokile Dec 07 '18 at 12:52
  • I understand that, but I can't update a record that does not exist, I've already understand that the `$log` query is wrong and thanks for advice me, But the other above it's working with no problems, I'm only having problems with creating the `$log` register – Carlos Santiago Dec 07 '18 at 12:57
  • I need to replace the `column1...` for the respective names, but what it's the `$q->execute(array($nomeVariable));` variable? The `FK` ? – Carlos Santiago Dec 07 '18 at 13:03
  • `WHERE nome = ? ` is variable holds the data of nome – Masivuye Cokile Dec 07 '18 at 13:05
  • `Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ativo' in 'field list' in C:\xampp\htdocs\gestao\Ativos\create.php:120 Stack trace: #0 C:\xampp\htdocs\gestao\Ativos\create.php(120): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\gestao\Ativos\create.php on line 120` – Carlos Santiago Dec 07 '18 at 13:09
  • looks like you don't have `ativo` in your columns – Masivuye Cokile Dec 07 '18 at 13:12
  • `ativo` is the principal field of `ativos` table. – Carlos Santiago Dec 07 '18 at 13:18
  • 1
    Let's say I needed to get both of my and your code, your `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) VALUES(?,?,?)"; $qlog = $pdo->prepare($log); $qlog->execute(array($acao_log2,$data_log,$id_ativo));` was totally right and that's what I was missing, thanks a lot for real and thanks for the advices too! – Carlos Santiago Dec 07 '18 at 13:27
  • sorry for bother u again, for the update will be a little different, right? – Carlos Santiago Dec 07 '18 at 17:47
  • 1
    @CarlosSantiago yes update is different check the 1st example – Masivuye Cokile Dec 10 '18 at 09:39