r/PHPhelp • u/DanceApprehensive564 • Dec 21 '24
How to efficiently update related database tables when editing song details in PHP?
Hi, everyone!
I’m working on a song entry form in PHP that affects multiple database tables. Here’s how it currently works:
- When adding a new song, entries are created in a main table and several related tables.
- However, when editing song details, I only update the main table, but for the related tables, I delete all rows and re-insert the updated data, even if no changes were made to those fields.
While this works, it feels inefficient, especially as the dataset grows. I’m looking for a better approach where:
- Only the modified data gets updated in the related tables.
- Unchanged rows remain untouched to reduce unnecessary database operations.
Would love to hear your suggestions or best practices for handling this scenario! Thanks in advance. 😊
1
Upvotes
4
u/paradoxthecat Dec 21 '24 edited Dec 21 '24
First question - is the data that is being changed duplicated across tables and if so why?
The edit should only affect one table, which has foreign keys to other tables, but each piece of data should only be in one table. Any editable data should be in the main songs table, and edits could include changes to the foreign keys as well (for example to associate the song with a different artist).
Sounds like you need to rethink your database schema first.
1
u/eurosat7 Dec 21 '24
Good thinking
1
u/colshrapnel Dec 21 '24
I have mixed feelings about this request. On the one hand, the task is certainly doable. And could constitute a great exercise. On the second hand, the task is certainly a premature optimization, and once completed, it will make your code more complex (means more error prone) or less performant. Or both. On the third hand, given you have no idea on "how to track" (or, rather, fancy the idea there is a way), your biggest problem is understanding how PHP works. And your immediate goal, instead of that "tracking", should be to learn that to the point where you can answer your questions. So start from asking yourself, in which part HTTP protocol is supposed to track changes in the data some other script sent to the browser.
3
u/colshrapnel Dec 21 '24
Finally I got your problem, mostly through other people comments. Here is what I did. My entity can have multiple addresses stored in another table (though just one to many as there is no way for address reuse) and this list can be fully edited on the client side. So there could be new records, deleted records, or records that stay. The input data is coming through API as a simple PHP array So I am selecting a list of current addresses and then just do array_diff():
I was too lazy to add a condition that compares $current['address'][$addr['id']] with $addr and only fires update if they don't match but you can add it as well
1
u/BarneyLaurance Dec 21 '24
You could consider using the Doctrine ORM or something like that. When you load data from the DB doctrine keeps a copy of it in memory, so that when you tell it to 'flush' any changes back into the DB later it can check against that copy and only do database operations for the modified objects.
1
u/martinbean Dec 21 '24
What exactly are these “other” tables?
You should be using transactions to update data in multiple tables to ensure you don’t leave your database in an inconsistent state, but this feels very much solving a problem of your own creation if your database schema isn’t optimal in the first instance. Deleting data on an update, just to re-insert it afresh, doesn’t sound the best approach to whatever you’re doing. It’s also going to cause issues for any tables relying on that data via foreign keys etc if you’re just constantly trashing records.
1
u/[deleted] Dec 21 '24
Save this optimization for when you need it.
1
u/PrizeSyntax Dec 21 '24
Just delete and insert, for small amounts of rows the additional complexity isn't justified.
Other wise you could do smth along the lines, load the old data, make a intersect/diff with the new data to delete not needed rows, then inser/replace remaining rows, if you have setup keys the right way in the table, but as I said it's finicky, complex and error prone, or some other complex comparing algorithm
1
u/MaxxB1ade Dec 21 '24
Everything else that everyone is saying is valid, however, please look up database normalisation. It was the last thing I learned on my database journey and it completely changed my thought process. It was the first part of my course and the last part I fully understood.
I knew every other part of the degree level course, except normalisation. You have no idea how much it rocked my world. Through the necessity of trying to work with my awful database designs, I learned left and right joins, indexes, multi-level queries and all kinds of other mad shit that you should really only do when all other options have been exhausted. Security was luck based (D20) and my life was miserable.
I'm not going to say that normalisation solved all my problems but it made approaching them a joy rather than a misery.
And, just because I think I am good at this, I know there are more people that are better.
Learning is the only kind of "trickle-down-economics" that works.
1
u/AmiAmigo Dec 22 '24
Isn’t this a database design problem?
OP mind giving one clear example?
What’s the main table? And what are the fields?
What are the other tables? Just give one or two? And their fields. Or screenshot your schema diagram
0
u/[deleted] Dec 21 '24
[deleted] — view deleted comment