Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:33:34+00:00 2026-06-17T18:33:34+00:00

I am looking for the best approach to save changes to a list of

  • 0

I am looking for the best approach to save changes to a list of checkboxes to the database. Here is my setup:

I have a list of checkboxes that are either checked or unchecked, based on some database entries, as such:

Animals I like: 
0 Cats 
X Dogs
0 Birds
X Elefants

Now the user can completely change his/her mind and select Birds and deselect Dogs and I want to save this change in the database as efficiently as possible.

The way the db is structured, I have a user table (with a user_id) and an animal table (with an animal_id). The “likes” are tracked in a pivot table (because it is a many-to-many relationship).

Here are a few approaches I have considered, but I am interested in any other/better/more efficient ones:

1) On save, delete all entries in the pivot table for this user and enter only the checked ones.

This has the advantage that I don’t need to compare much of the before/after choices. The disadvantage is that I delete an entry that has not changed (i.e. elefants in the above example). If I attach a creation timestamp for example to the elefant like, it will change every time, even when I don’t change that selection

2) On save, I query the db to get a list of all original likes. The I compare this list to new the new likes. Every time I encounter an original like, that is not in the new list, I remove it. If I encounter a new like that is not in the original list, I add it.

This has the advantage of only changing the changes to the db, but it seems like an awful lot of queries. If the list of animals is long and many changes are made, the looping could result in a lot of db transactions.

So, what would be the best practice to solve this issue. I mean, it must be a common problem and I don’t want to reinvent the wheel here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:33:36+00:00Added an answer on June 17, 2026 at 6:33 pm

    option 1 would be the best, but since you don’t want your timestamps disturbed, you CAN do a somewhat more efficient system than “check each record individually”.

    1) fetch a list of the user’s choices from the db, $original.

    2) fetch the list of choices from the submitted form, $submitted.

    3) use array_diff() to figure out what’s changed and what you need to do to the database:

    e.g.

    $original = array(2,4,6,8);
    $submitted = array(4,7,8);
    // so 2 and 6 have been removed, and 7's been added.
    
    $unchanged = array_intersect($original, $submitted); // 4, 8
    $removed = array_diff($original, $submitted); // 2, 6
    $added = array_diff($submitted, $original); // 7
    
    $sql = "DELETE FROM pivot_table WHERE animal_id IN (2, 6);"; // remove 2&6
    $sql = "INSERT INTO (animal_id, ...) VALUES (7, ...)"; // insert 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for best approach to handling database connection. I have designed my
Looking for the best approach for a non-image CSS Drop Shadow technique. I have
i'm looking for a best approach to the following problem. I have an array
Im looking for the best approach to takle the following, I have sorted a
I'm looking for the best approach to comparing files that I believe are identical
Looking for the best approach: I have a tabbar and a navigationbar. under the
I'm looking for the best approach for generating data transfer objects from business objects
I am looking at the best possible approach to search and replace for a
Looking for best advice on how to do this: I have an insert like
I'm looking for best practices here. Sorry. I know it's subjective, but there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.