Jump to content

Insert, Update & Delete Checkbox Selections


biggieuk

Recommended Posts

Hi,

 

Looking for some help with this slight specific mysql query.

 

I have a table `services` which looks something like:

 

id

servicesid

articleid

1

5

10

2

7

20

3

5

16

 

This is used so that i can return all of the articles tagged to a specific service (using serviceid).

 

My ui has the articles selectable as checkboxes.

 

What is the best way to skip the rows that have not changed, delete the rows that have been deselected and insert new rows for newly selected checkboxes?

 

An 'ON DUPLICATE KEY' command will not work as there serviceid & articleid can be used more than once?

 

Thanks for help with this. :confused:

Link to comment
Share on other sites

Im guessing my description wasn't too good.

 

I'm currently having to delete all records with the matching servicesid then input all the selected id's.

 

There must be a cleaner way than this?

 

I would normally try and perform an update and check if any rows were affected before inputting but this also requires a unique ID which I am not passing into the query.

Link to comment
Share on other sites

When I have something like this, I query all the options, then loop through the results, either deleting the row if the checkbox doesn't exist or replace (or insert, which if you have constraints to prevent duplicates would prevent you from overriding the existing rows if there is data there that you don't want to have to recreate).

Link to comment
Share on other sites

While servicesid can appear in the table more than once; and articleid can appear in the table more than once; I suspect that the combination of servicesid and articleid needs to be unique. You can create a unique index on these two fields (combined) - in addition to your existing primary key. Then INSERT IGNORE DUPLICATE KEY should work. It is my understanding that these phrases do not care which unique key is being duplicated.

 

On removing rows: If what you have is the articleid's that need to be left in the table you can

$list = implode(',', $_POST['articleIDs']);
$sql = "DELETE FROM services WHERE servicesid = 5 and articleid NOT IN ($list)";

or something along those lines

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.