简体   繁体   English

创建日志表以注册记录MySql PHP

[英]Creating log table to register records MySql PHP

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. 我正在尝试为我的数据库创建一个log表,以log发生的所有操作,并且我正在考虑为每个表创建一个日志表,以便能够轻松读取信息。
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. 但是我不确定该怎么做,因为我希望log表的输入为一个变量,在其中我将格式化文本并放置其他变量来注册输入。
Eg: $log = '[' . $data_log . '] ' . ' The item '. $ativo .' with the S/N '. $numero_serie .' was delivered to the employee '. $id_colaborador); 例如: $log = '[' . $data_log . '] ' . ' The item '. $ativo .' with the S/N '. $numero_serie .' was delivered to the employee '. $id_colaborador); $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 致命错误:未捕获的PDOException:SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与C:\\ xampp \\ htdocs \\ gestao \\ Ativos \\ create.php:129中的令牌数量不匹配:堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ gestao \\ Ativos \\ create.php(129):PDOStatement-> execute(Array)#1 {main}抛出在第129行的C:\\ xampp \\ htdocs \\ gestao \\ Ativos \\ create.php中

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. SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, id_colaborador这个真正错误的表和列名称不能用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. 上面的查询是错误的。INSERT不支持where子句 ,而是您正在查看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 花一些时间做sql需求教程,并与php manual和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, 我想根据第一个查询将生成的id_ativo为该表创建一个新的日志记录,

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. 然后,当成功插入第一个语句时,您需要做的是获取最后生成的插入ID ,并在下一个插入中使用该ID

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));

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM