views:

32

answers:

2

Quick background info: of the three database tables that are relevant for this question, one holds devices info (d.id, d.name), the second one holds accessories for the devices in the first table (a.id, a.name) and the third table handles relations between the first two (d.id, a.id).

By selecting one of the available devices from a drop-down box, a list of all accessories is being displayed, each with a checkbox which is either checked or not, based on the data in the relations table.

And this brings us to my problem: I am not sure what is the most efficient way of checking which checkboxes were changed after the form has been submitted, as well as what the most efficient way of querying the database would be, since both INSERTs and DELETEs might be required for the same submitted form (i.e. a users un-checks a checked checkbox and checks an un-checked one -- whoa, that phrase should be a diction exercise -- before submitting).

By the way, I'm not hell bent on using checkboxes, but I figured it would be a clean way to handle this. Any suggestions and proof-of-concept code would be more than appreciated !

Thank you all in advance !

+1  A: 

Look into REPLACE INTO on your bridge/join table between products and accessories. It's an under used gem in MySQL.

Otherwise, as you said, you can DELETE then INSERT everything. But if you have any additional foreign keys, this could get messy. However, in the end it seems to be the most common way, for better or worse.

If you did have foreign keys, you could pass an additional field containing the ids of previously checked accessories and handle those accordingly by comparing them to the submitted form data. I'd only suggest doing this if you have other transactional data in your bridge table that is important.

Jason McCreary
Thanks for the suggestion ! I've never used `REPLACE INTO` before, so I'll definitely look into it. There are no foreign keys, the tables are pretty much as simple as I described them. However, I am still unsure how to check which checkboxes got changed before building the DB query. I was thinking of passing their initial state as `1` and `0` somewhere in their `name` attribute but it seems like a bit of a kludge, and I was thinking that maybe there is a more elegant way of doing it.
FreekOne
In the end, there's not need to get elegant if these tables are as you describe them. Just `DELETE` and `INSERT`. It's primitive, but gets the job done and if you look at any framework, that's what they do under the hood. Not that it makes it any better ;)
Jason McCreary
+1  A: 

If you have a "Submit" button below the checkboxes, just send the form to the server with POST or GET, whatever you require.

Use AJAX and onClick with the checkboxes if you want to change the database on the fly. (When a user unchecks, send changed checkbox value('false') to the PHP script, it will then make a DELETE query. If the value of checkbox is 'true', it will fire an INSERT query).

For AJAX sending you could use jQuery(would end up in less code)

Jevgeni Bogatyrjov
The funny thing is that although the application relies heavily on jQuery and AJAX, it never occurred to me that I could handle the relations one by one, on the fly. Thanks for the suggestion !
FreekOne
Nothing against this approach. But it should be noted that you just added a dependency on a front-end technology to manage your data relationships. Although 99% of desktop browsers support JavaScript, there are many other devices that don't. You should still solve your original problem - submitting the form - then add the above progressively.
Jason McCreary
@Jason McCreary - I completely agree. This however is for an admin panel for a custom software, not publicly available, so there's no worrying about browsers/devices that do not have JS enabled.
FreekOne
I understand and that's fair. I know sometimes things just need to get done. But a good developer still builds it *right*. I'm off the horse now ;)
Jason McCreary