I need to be able to determine what database fields were changed in a mysql update and then log that change to another db table to eventually display to the user.
This application is for a system that tracks customer orders. It's developed in CodeIgniter, but I don't think that's necessarily relevant unless CI has some built-in library that could be helpful.
For example, here is where the database update occurs:
$this->db->update("orders",$this->input->post());
I can easily follow this up with an insert on the "events" table that says something like "Order Updated", but I have no way to determine what part of the Order was actually updated. I would like to be able to instead log something like "Order Status Updated" or "Order Quantity Updated".
How can I determine which db fields were changed in the mysql update? The answer doesn't have to be in CodeIgniter...raw php/mysql is fine.
Related
I have a question, some might have encountered while working with data. Trying to figure out how to implement "signature" for data passed back and forth to SQL Server via PHP.
For example, I have a sales order that contains dates, numbers, other variables (string), etc.
The idea is that when that data is inserted into SQL, a "signature" is created based on all the variables (around 30-40) and stored in that table. That signature will be sent to the user when user pulls the data. Then, when a user updates that data and sends back to SQL along with the current signature, PHP would compare that current data signature with the one stored in the database and update the data if it matches. if not - the user will get a notification saying something like "the state of the order has been changed, new data available".
This will prevent one user from overwriting the data if that data has been changed by another user while the first user worked on it.
I thought to add "user signature" which is comparatively easy. However it does not resolve the situation when the same user has opened multiple windows with that data and then accidentally tries to submit updates from one window after he already submitted some data from another window. (Some users are old folks and not tech savvy and may have open multiple windows in the browser, you know).
Locking records won't work here as the data has to be accessible by multiple users at any time.
Thank you for your help!
NOTE: If someone is using SQL Server Management Studio, seems like you can add this column only via query. At least I coud not find this option in "design" view.
ALTER TABLE table_name ADD Version rowversion;
I’ve a problem that I don’t seem to find on the internet. I was trying to create a website where users transaction histories are saved on the database. Let’s say after a new user signs up and login and then starts to buy or sell stuff from the site, their past transactions should be saved in the database, so whenever they want to view their transaction it will show them their transactions histories. If they have 1, 3 , 12 or more transactions history it will show them everything. My problem now is I don’t know how to do it or even start. It should be able to look a table for each user.
Please make one more table with required transaction columns with post-fix of tablename_history. Use primary key with Auto increment for reference. i.e history_id.
But important thing is that you need to make separate transaction to Insert and Update Query to avoid performance issue.
I have a Mysql database which contains the status of many orders and where they are located. Im looking for a way to notify my customer on the update of the status of their order.
Basically everytime the column "status" would be changed ( could be done with a trigger of some sort ) , trigger an event that would somehow be able to call an external script (preferably PHP) to either text or email my customer that their order has been updated and that the status is now X, if completed , tell them to come pick it up.
I've read around that triggering script from a mysql trigger is extremely bad pratice but I'm wondering if there are other options to do something like this.
Any suggestions?
I currently maintain a DB table of users, when after logging in I update the table with their ID and login_time. This works to a point but currently I can't tell if the user has been active since the login or for how long.
Is there a better way to get a complete list of users that have been active in the past X minutes?
The best way to get what you need would be a "Last Activity" column in the users table. You would just update it whenever a user access a page. Depending on what information you need it could replace the login_time column or it could be a new column.
You'll have to keep track of when the user made their last request in your database as a separate table or column. You can then formulate a query to select, e.g. all users that have made a request in the last 5 minutes.
PHP itself does not store - or care for - that kind of information. Unless you happen to have your own session management module which does store this kind of information, then you could use data from that.
I want to notify users of changes on a site. Users are subscribed to different kinds of changes so I don't send all changes to all users.
Here's what I am thinking: at t=0 (i.e. an if statement that checks that some table is empty) I basically have an SQL query that fetches the appropriate changes and mails to the appropriate users. I then populate a user_changes table which essentially stores what changes have been mailed to what users. Mailing is done with the php mail function
Then at t>0, I run the SQL query again but this time with the condition that changes+user are not in the user_changes table. The main issue i see with this is that the user_changes table could get large very quickly. Is this a problem? I am trying to avoid hacks that use dates to filter stuff, since I want new users to be able to receive old changes that are relevant to them.
Appreciate suggestions.
How about having one entry per user, and a record of the last sequence number of updates? when you send the email updates, update the record with the latest and greatest. Your table should be sized with your user base, then.