How do I get the data before the PDO update?

Hello,
I am creating log records of which user did which actions.

UPDATE urunler SET stok = stok - ? WHERE id = ?

How do I get the stock count before stock count goes down?

Is it possible without using SELECT * FROM?

Thank you

If you are doing this for real, you would NOT update a column in a table to keep track of a value, since it doesn’t provide an audit trail that would let you know if a programming mistake, duplicate form submission, or nefarious activity altered the value.

Instead, you would INSERT a row for every transaction that affects the value, just like your bank, credit card, loan, utility, or any other account does.

However, since there are some actual use cases to get either an initial value, final value, or id of the altered row from an UPDATE query, you would use the MySql LAST_INSERT_ID(expr) function in the query, where expr is the value you want. After you execute the UPDATE query, you would then use the PDO lastInsertId() method to access the value.

1 Like

Thank you for the answer,
I’m an amateur so I don’t quite understand what you mean.
What I’m trying to do is a different stock program.
I have 10 sql tables and I want to save all changes made in each table and each column in a separate table.
-Adding new data,
-Delete data,
-Data editing
logs table
User who made the change
Old data
New data
Date

There are a couple options for Auditing. One option is using triggers. There is also an audit plugin which I have never tried.

Here is one tutorial on trigger auditing.
https://www.thedeveloperfriend.com/database-scripting/implementing-mysql-table-audit-trail-using-triggers/

1 Like

Thank you so much
Exactly as I want
works perfectly
I can track all transactions.

Sponsor our Newsletter | Privacy Policy | Terms of Service