-2

I've got a basic CRUD application that is running just fine. I want to add an entry to a log when something changes -- someone adds a record, for example.

When someone updates an existing record, I want the log to show only what changed. For instance, I've got the following fields:

Name
Address
City
State
Zip

So if the address changes (and ONLY the address), I'd like the log to say:

'Address changed from YYY to ZZZ'

Here's my current statement to write the log. What do I need to change?

INSERT log SET user_id = '$logid', username = '$logname', usertime = '$logdate', ipaddress = '$_SERVER[REMOTE_ADDR]', action = 'update', name = '$name', address = '$address', city = '$city', state = '$state', zip = '$zip'
Mr_Thomas
  • 857
  • 3
  • 19
  • 39

2 Answers2

0

The syntax is INSERT INTO your_table... but you are describing an update which would be: UPDATE your_table SET var1='new value' WHERE condition. As far as handling what change was made you can do that in PHP with IF, ELSEIF, ELSE.

user3741598
  • 297
  • 1
  • 12
0

I would use a MYSQL TRIGGER to detect insert, update or delete and MYSQL UDF to write to some external log file. You would need to write additional trigger sql statements. Your sql statement only solves part of your issue. Research MYSQL TRIGGERS and UDF to achieve your goal

MySQL Triggers: http://www.w3resource.com/mysql/mysql-triggers.php#MST MySQL UDF: Mysql trigger write data to file

Community
  • 1
  • 1
zbot
  • 36
  • 3