Note that there are some explanatory texts on larger screens.

plurals
  1. POpostgres and php: controlling changes done to a table
    primarykey
    data
    text
    <p>I'll try my best to explain my problem in english. I created a database called Laboratory in which I want to control when a user inserts, deletes or updates a table. I called this table "tablaLog".</p> <p>Basically tablaLog is updated by a trigger. It saves the information about who changed a determined table, the old data, the new data, the date, the user and the pid. This is where I'm having the problem. The pid is the problem. Our teacher said that when, lets say an insertion is made (using a php form) we should look for its pid on the table "detalle_sesion" so we can now which user is making the change. Here it is the function I wrote for this:</p> <pre><code>CREATE OR REPLACE FUNCTION escuchar_cliente() RETURNS trigger AS $BODY$ DECLARE res integer; p int; BEGIN p = pg_backend_pid(); SELECT pid INTO res FROM detalle_sesion WHERE pid = p; IF res IS NOT NULL THEN DECLARE cod int; nombre character varying(20); BEGIN SELECT cod_usuario INTO cod FROM detalle_sesion WHERE pid = p; SELECT login INTO nombre FROM usuarios WHERE cod = cod_usuario; IF (TG_OP = 'DELETE') THEN INSERT INTO tablaLog (fecha, datoviejo, datonuevo, operacion, usuario, tabla, cod_tabla, cod_usuario) VALUES(now(), OLD.*, null, 'eliminar', nombre, 'Cliente', default, cod); RETURN OLD; ELSIF (TG_OP = 'UPDATE') THEN INSERT INTO tablaLog (fecha, datoviejo, datonuevo, operacion, usuario, tabla, cod_tabla, cod_usuario) VALUES(now(), OLD.*, NEW.*, 'actualizar', nombre, 'Cliente', default, cod); RETURN NEW; ELSIF (TG_OP = 'INSERT') THEN INSERT INTO tablaLog (fecha, datoviejo, datonuevo, operacion, usuario, tabla, cod_tabla, cod_usuario) VALUES(now(), null, NEW.*, 'insertar', nombre, 'Cliente', default, cod); RETURN NEW; END IF; RETURN NULL; END; END IF; RETURN NULL; END; $BODY$ LANGUAGE plpgsql </code></pre> <p>I can tell this function is not working since well...I doestn't inserst nothing on tablaLog. Some classmates are having the same problem and they say is due to the pid, that everytime a query is made a new pid is generated and it's not the same pid that is was saved on "detalle_sesion".</p> <p>The table named "detalle_sesion" updates everytime a user logs on [cod_usuario, cod_sesion, ingreso, salida, pid] Here is how the table is updated.</p> <pre><code>CREATE OR REPLACE FUNCTION detallesesion(logi character varying, pass character varying) RETURNS boolean AS $BODY$ DECLARE res character varying (20); pid int; cod int; BEGIN SELECT cod_usuario INTO res FROM usuarios WHERE login = logi AND password = pass; IF res IS NOT NULL THEN DECLARE cod int; pid int; bs timestamp with time zone; qs timestamp with time zone; BEGIN cod := CAST(res AS integer); pid = pg_backend_pid(); SELECT backend_start, query_start INTO bs, qs FROM pg_stat_activity WHERE procpid = pid; INSERT INTO detalle_sesion(cod_usuario, cod_sesion, ingreso, salida, pid) VALUES (cod, default, bs, qs, pid); RETURN(TRUE); END; ELSE RETURN(FALSE); END IF; END; $BODY$ LANGUAGE plpgsql </code></pre> <p>About php, I have the next: conexion.php</p> <pre><code>&lt;?php $conexion = pg_connect("host=localhost port=5432 user=postgres password=postgres dbname=labo") or die ("no se pudo conectar".pg_last_error()); ?&gt; </code></pre> <p>manejadorDB.php All the functions with queries are here. </p> <pre><code>&lt;?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once "../funciones/conexion.php"; class manejadorDB { </code></pre> <p>So I'm asking if there is a way to mantain the same pid since the user is logged on. Or maybe a different way to do the update of "tablaLog"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload