Neil,
Thanks for sharing your modified version. I had also modified the code a bit so that you can log deleted records as well.
Either you call insertLog() in the "Before Save"-event with insertLog("","update");
or in the "Before-Delete"-event with insertLog("","delete"). In the latter case, all "log_after"-values are set to NULL and the additional column "log_type" will contain "delete"
Feel free to use parts of it...
Code: Select all
function insertLog($form, $type) {
// $form: if empty: main form, otherwise subform
// $type: update, delete
$o = nuSubformObject($form);
$u = "#USER_ID#";
for($i = 0 ; $i < count($o->rows) ; $i++){
$p = $o->rows[$i][0];
$s = "SELECT * FROM $o->table WHERE $o->primary_key = '$p'";
$t = nuRunQuery($s);
$r = db_fetch_array($t);
for($I = 1 ; $I < count($o->rows[$i]) ; $I++){
$N = nuID();
$T = $o->table;
$F = $o->fields[$I];
$P = $p;
$U = $u;
$B = $r[$o->fields[$I]];
$A = $o->rows[$i][$I];
$Y = $type;
if ($A !== $B) {
$S = "INSERT INTO my_log_table (my_log_table_id, log_type, log_table, log_field, log_pk, log_before, log_after, log_user_id)
VALUES ('$N', '$Y', '$T', '$F', '$P', '$B',". (($Y=='delete')?"NULL":("'".$A."'")).", '$U')";
nuRunQuery($S);
}
}
}
}