This topic is locked

Insert or Update Logged User and other data in other Table

8/2/2007 4:52:36 PM
PHPRunner General questions
H
hugolesme author

I have a User table
idusuario serial NOT NULL, -- ID y Clave Primaria del Usuario

"login" character varying(20) NOT NULL, -- Login o Nombre de Usuario

password character varying(15) NOT NULL, -- Contraseña del Usuario

idfunc integer NOT NULL, -- ID del Funcion del Usuario

crea timestamp without time zone NOT NULL DEFAULT now(), -- Fecha de Creacion del Registro

mod timestamp without time zone NOT NULL DEFAULT now(), -- Fecha de Modificacion del Registro

email character varying(50), -- email del usuario

idsec integer NOT NULL DEFAULT 1, -- Seccion del Usuario

idrol integer NOT NULL DEFAULT 2, -- Rol del Usuario
and Product Table
idproduct serial NOT NULL, -- Clave Primaria del Articulo

productdesccharacter varying(250) NOT NULL, -- Descripcion del Articulo

crea timestamp without time zone NOT NULL DEFAULT now(),

mod timestamp without time zone NOT NULL DEFAULT now(),

obs text,

idusuarioreg integer NOT NULL,

idusuariomod integer ,

loginreg character varying(200) NOT NULL,

loginmod character varying(200) ,

remote_hostreg character varying(200) NOT NULL,

remote_addrreg character varying(40) ,

remote_hostrmod character varying(200) NOT NULL,

remote_addrmod character varying(40) ,
When Insert a Product, How can I get:

Login name

ID of the person

remote_addr

remote_host

who is logged in, and put it into idusuarioreg field, loginreg field , remote_addrreg and remote_hostreg in the Product Table?
When Update a Product, How can I get:

Login name

ID of the person

remote_addr

remote_host

current date time

and put it into idusuariomod field, loginmod field , remote_addrmod, remote_hostmod and mod field in the Product Table?
Hugo

V
Vladimir 8/3/2007

Hi,
you can use this code in the Before record added and Before record updated events in the Events tab.

Before record added event:

global $conn;

$strSQLtmp = "select login from `User table` where id = ".$_SESSION['UserID'];

$rstmp = db_query($strSQLtmp,$conn);

$datatmp = db_fetch_numarray($rstmp);
$values["idusuarioreg"] = $_SESSION["UserID"];

if ($datatmp)

$values["loginreg"] = $datatmp[0];

$values["remote_addrreg"] = $_SERVER['REMOTE_ADDR'];

if(!$_SERVER['REMOTE_HOST'])

$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);

else

$host = $_SERVER['REMOTE_HOST'];

$values["remote_hostreg"] = $host;
return true;


Before record updated event:

global $conn;

$strSQLtmp = "select login from `User table` where id = ".$_SESSION['UserID'];

$rstmp = db_query($strSQLtmp,$conn);

$datatmp = db_fetch_numarray($rstmp);
$values["idusuariomod"] = $_SESSION["UserID"];

if ($datatmp)

$values["loginmod"] = $datatmp[0];

$values["remote_addrmod"] = $_SERVER['REMOTE_ADDR'];

if(!$_SERVER['REMOTE_HOST'])

$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);

else

$host = $_SERVER['REMOTE_HOST'];

$values["remote_hostmod"] = $host;

$values["mod"] = now();
return true;