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

  • SEARCH
  • Home
  • 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 7977835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:19:24+00:00 2026-06-04T09:19:24+00:00

Multiselect option For example, a user with the id of ’10’ has the option

  • 0

Multiselect option

For example, a user with the id of ’10’ has the option to choose what languages he speaks.
He chooses each language using a “multiple select” or “multiple checkboxes”, like these:

<input name="lang[]" value="en" type="checkbox" />
<input name="lang[]" value="es" type="checkbox" />
<input name="lang[]" value="jp" type="checkbox" />

What I want to know is, how does the db table that stores these options look like and how would the server side php insert/update them?

My guess so far

What I’m imagining is that the table will look something like this:

CREATE TABLE user_langs (id INT AUTO_INCREMENT PRIMARY KEY, lang VARCHAR, fk_user INT);

While to INSERT the values into a new user, php does a simple insert loop:

$stmt = $pdo->prepare('INSERT INTO user_langs (lang,fk_user) VALUES(?,?)');
foreach($_POST['lang'] as $lang){
    $stmt->execute(array($lang, $user_id));
}

The problem I’m getting is with UPDATES, the simplest way would be to delete all the existing entries of this user, and insert the new ones.

$stmt1 = $pdo->prepare('DELETE FROM user_langs WHERE fk_user=?');
$stmt1->execute(array($user_id));
$stmt2 = $pdo->prepare('INSERT INTO user_langs (lang,fk_user) VALUES(?,?)');
foreach($_POST['lang'] as $lang){
    $stmt2->execute(array($lang, $user_id));
}

But I think this will over increment the primary id too quickly if it’s actively used, even if the upper limits of the id are astronomical I don’t like the idea of polluting my database, so I’m guessing I’m doing something wrong, so I would like to know how the pro’s handle it.

  • 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-04T09:19:26+00:00Added an answer on June 4, 2026 at 9:19 am

    This problem seems to come from the fact that the id in the above example is artificial, the solution is to use multi-column primary keys also called Compound key which I was unaware about when I asked the question.

    The solution then becomes to use a table like this:

    CREATE TABLE user_langs (
      lang VARCHAR, 
      fk_user INT, 
      PRIMARY KEY(lang,fk_user)
    );
    

    This improves data integrity because a single user can no longer have 2 entries of the same language.

    To insert a value you do the same:

    $stmt = $pdo->prepare('INSERT INTO user_langs (lang,fk_user) VALUES(?,?)');
    foreach($_POST['lang'] as $lang){
        $stmt->execute(array($lang, $user_id));
    }
    

    And the simplest way to handle an update is to delete all entries bound to this user, and insert the correct ones again in the same manner:

    $stmt1 = $pdo->prepare('DELETE FROM user_langs WHERE fk_user=?');
    $stmt1->execute(array($user_id));
    $stmt2 = $pdo->prepare('INSERT INTO user_langs (lang,fk_user) VALUES(?,?)');
    foreach($_POST['lang'] as $lang){
        $stmt2->execute(array($lang, $user_id));
    }
    

    You can also do it like zerkms suggests, to SELECT all the languages, then run array_diff to find the old and new values, and delete the old ones and insert the new ones, but this means running 3 queries and comparing results, while in case of languages it is very rare for a user to speak more than 3, which is why just deleting and inserting again seems like the best option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I got a multi select box <select id =countries name=countries multiple=multiple> <option
I am using a multiselect with options grouped together. <select title=Fruits multiple=multiple id=fruits name=fruits[]>
I am using jquery multiselect plugin, If an option has been selected i would
I am using jquery multiselect in my project,when user clicks on checkall then how
I have a page with several multiselect drop-down menus. Each menu has a general
I am using a jquery multiselect plugin http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ with option. All works fine, however
I have select <select multiple id=select2> <option value=1>1</option> <option value=2>2</option> <option value=3>3</option> </select> And
I'm using the ui-multiselect widget to render a <select> list differently. I'm trying to
I have a form like this: <form action=/cgi-bin/cgi_info.py method=POST> <select id=faults class=multiselect multiple=multiple name=faults[]>
Let's say I have a grid with multiselect option on, when user selects 4

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.