How do I save to the database, when a "user" or "admin" edit or register a new item? [closed]

0

I want to know how to save the actions, type, when admin, logs and shifts, changes a register or creates a new cadastre ??

    
asked by anonymous 15.06.2018 / 04:18

2 answers

0

By what I'm seeing you want to create log type I have something similar in my script 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 this type:

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

Just a note if you are going to use this just for learning and testing ok but if it is for something more serious I recommend improving the INSERT make it safer to not take sql injection example if you use the pdo pass the data to bindparam I know it's just a more secure log function first, then just create a paging with the logs

    
15.06.2018 / 05:22
0

I think what you are looking for is this Triggers
You can create a new log table and each insert, delete or update the trigger created in the database automatically adds in the logs table 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;

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

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

- "For Each Row" says Trigger Should Run this command for each new insert

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

- The Reserved Word "new" refers to the new record that was inserted, in the example it will insert into the record table the name of the new customer. It could also be used "old" to get the old value of an update.

To understand better read here

    
15.06.2018 / 04:52