How do I save to the database when a "user" or "admin" edits or registers a new item?

Asked

Viewed 53 times

0

I want to know how to save the actions,type, when admin, log in and drop, change a register or create a new register??

  • Example: In the same way that you authenticate by searching the database, you make a insert in a table "log" with the data you want.

  • I still can not understand very well about the "log", I’m doing several searches, but lost

  • Regarding "log", I have to make a query in the table of users before doing the Insert in the table logs?

  • Dude, logging is just information. Just as you have a "registration" record, which you enter what you want, "logging" is the same thing. There where, at what moment and what data you will pull, you set it. Or you can make a Rigger, if that will suit you.

  • Trigger in case, I just save the changes made right in the right bank ? I want to save the actions they make on the pages...

  • So... it could be by Trigger or by query. But I think you’re missing knowledge in bank theory, triggers. Your question is broad but at the same time simple. They have already shown the 2 forms in the answers.

  • Got here rs, thought better and worked out, my problem now is with the date, which is saving 0000-00-00 00000

  • Oops, cool! See which of the answers helped you, mark as solved by clicking on the " V ". To help you with this date, ask a new question, show the code how it is, detail well that the staff surely answers this quickness! Good luck with your project!

Show 3 more comments

2 answers

0

I think what you’re looking for is this Triggers
You can create a new table of logs and every Insert, delete or update the Rigger created in the database automatically adds in the table logs the change made and the date for example.

-- In your case it would look something like this:

CREATE TRIGGER NOME_TRIGGER 
AFTER INSERT ON TB_CLIENTE 
FOR EACH ROW
BEGIN
INSERT INTO TB_REGISTROS SET CAMPO_DA_TABELA_REGISTROS = NEW.CLIENTE_NOME;
END;

-- After(after) above means that Trigger will be executed after insertion -- You could also use before(before) for Trigger to run before Insert,update or delete

-- "Insert on" is saying that Trigger will be triggered when an Insert occurs in tb_client -- Could also be used "update on" or "delete on"

-- "For Each Row" says that Trigger Should Execute this command for each new Index

-- The "Begin" word marks the beginning of the command and the "end" word.

-- The Reserved Word "new" Refers to the New Record That Was Inserted, in the example it will insert in the Record Table the name of the new client. Could also be used "old" to get the old value of an update.

To understand better read here

  • In case I have a table of users and a table of client registrations, I create one more table "records" and then use this triggers, in the Inserts or updates, of the user tables and registers ?

  • I just wanted to log into a table, every time an employee logs in and drops out of a system, and also save any changes he makes to that system...

  • I made the changes in the reply, if you have not understood, access the link I put there, will explain you in more detail.

  • Can I use it in two tables ? For example, as said, I have the two tables, that of users that contains: id, name, user, nivel_usuario('admin','user_common'),status('active','inactive')... and also the table, registration, which contains the id, id_client, informacao_client and etc, ai wanted to make the junction that picks up, id, level, and name of the user who made the change in the client

  • With Trigger you can record the changes made to the tables automatically in the database itself, but since it does not accept parameter there would be no way you know who edited such record, but you can still try with Stored Procedures, but it would be a more manual job and you would have to call the Procedure by name in php, like this $con->query("minhaProcedure($parametro)");

  • @Urilo Take a read on Stored Procedures

  • Sure, I’ll read it right now, thanks !!

Show 2 more comments

0


by what I’m seeing Voce wants to create log type I have something similar in a script of mine and this is how it creates the function :

function addlog($msg)
{
global $pdo;
$msg = strip_tags(htmlspecialchars($msg));
$pdo->exec("INSERT INTO fun_log SET msg='".$msg."', data='".time()."'");
}

then just create the table :

CREATE TABLE `fun_log` (
`id` int(255) NOT NULL auto_increment,
`msg` text NOT NULL,
`data` varchar(100) NOT NULL,
 PRIMARY KEY  (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

When you want to register, just call it like that :

$msg = "Fulano fez tal coisa";
addlog($msg);

only an observation if you will use this only for learning and testing ok but if it is for something more serious recommend improving the INSERT leave safer not to take sql example Injection if you use the Pdo passes the data to the bindparam know that it is only a log function more safety in the first place, then just create a pagination with the logs

  • Thiago, I don’t understand very well, I’m new in this area. This script you sent, it keeps these records in the database ?

  • exactly it record what the user does but only when you call example you have a page panel and you want to know when users enter it there is kind of like this : <?phpif($pg=="painel") { $msg="fulano entrou no painel"; addlog($msg); .......}?>

  • I think I got it, I’m just going to do a little research on Trigger because I found it very interesting how to create a history table with him, but I’ll try to do it the way you did too, thanks for the reply Thiado !

  • Trigger it works only with database if it’s just what you want is a good option more if you want more specific information like I said would be a good option like this

  • Ah yes, I understood Thiago, I’ll try this way, and I’ll give you a return !

  • Thiago, I went back to... Not being able to save anything in the log table, I also wanted to save everything by column, example: id_usuario[name of the user who made the change], nivel_usuario[level of the user who made the change], changed card[number of the changed card] and so on, you can do so?

  • of course there is only after executing change call the function to call the user name I have created a function that identifies the user id and shows the name, simple thing only a select even then add this function to a variable ex: $nome = getuid($uid); so if I want to just call the function $msg = "$nome modificou tal coisa" ; addlog($msg) ; if not right calling $name try %name%

Show 2 more comments

Browser other questions tagged

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