Reputation: 2022
I've got a form that allows the user to edit an employees information: name, email, technician level, etc. Well this is not hard in itself, I'm trying to find a good method for updating phone numbers for each employee. I have two tables (that are relevant): technicians and phones. My form allows the user to add, remove and edit any amount of phone numbers for the employee they are currently editing.
I'm not quite sure how to balance the updating of the phones table between PHP and JS (note this app is AJAX intensive).
I'm thinking I can either:
What would a common method of accomplishing this kind of task be (I'm sure this problem doesn't apply to only my situation). After typing it out, it seem to me the simplest way would be to just delete all the old rows for that technician and insert the new ones.
Upvotes: 0
Views: 904
Reputation: 12599
Idea #2 is the best one. Do it all in PHP. Comparing is expensive and slow. You have to query, perform a set difference and then generate some SQL.
I recommend deleting all existing phone numbers and adding all known good phone numbers.
DELETE FROM phones WHERE technician_id = 123; INSERT INTO phones (technician_id, phone_number) VALUES (123, '401-555-1212'), (123, '402-555-1212');
Upvotes: 0
Reputation: 44307
Deleting all the old rows and creating new ones might seem like a quick solution, but don't go there - you'll end up tying yourself in knots as soon as anything else tries to associate to the same phone numbers in a future version of the app.
Working out which numbers to add and which to delete is pretty simple to achieve, so I'd suggest doing the work on your server.
Upvotes: 1