I want to log changes (updates) on the 'users' table in a logs table.
I haven't tried but I thought about running a second query that inserts data on the logs table every time I update something in the users table.
It is a viable solution but I would have to run a query to get the old values, run another query to insert the users update and finally insert the old values in the logs.
This is how I update data on users:
$sql = "UPDATE users SET username = '$newUserName' WHERE username = '$userName'";
$res = $conn->query($sql);
if ($conn->query($sql) === true) {
$_SESSION['username'] = $newUserName;
echo "Username changed.";
} else {
echo $conn->error;
}
I want to take the old value in the users table, (the primary key of the row that has been updated, called user_id), the table and field affected, and the value that actually chagned.
So for example, I update users.username in the row with test@1234.test as primary key from Alice to Bob, the row in logs will have test@1234.test as user_id, users as table and username as field, with Alice as old_value.
How can I do this with triggers?