PDA

View Full Version : Adding member to secondary usergroup via SQL query


Easelm
01-23-2015, 07:14 PM
I am working on an offsite script and everything is working fine, but I am
having some problems figuring out the best way to add a member to a
secondary usergroup if they are already in another through mysql query.

I understand I can simply update or insert my data directly in to the membergroupids field but I am
asking how I can do this if they are in another secondary usergroup already, since they list
separated by commas.

So what's the best way to execute a query where I can add the new groupid to
membergroupids if the member has more groups already or none?

Dave
01-23-2015, 07:23 PM
You can do that with an IF/ELSE case in SQL:

UPDATE user SET membergroupids = CASE WHEN membergroupids = '' THEN 3 ELSE concat(membergroupids, ',3') END WHERE userid = 5

Where 3 is the usergroupid.

Easelm
01-23-2015, 07:59 PM
You can do that with an IF/ELSE case in SQL:

UPDATE user SET membergroupids = CASE WHEN membergroupids = '' THEN 3 ELSE concat(membergroupids, ',3') END WHERE userid = 5

Where 3 is the usergroupid.

Thanks, that makes it a lot easier for me


Update: Nice, it worked perfectly in my situation. Thanks again

Medi0cr3
01-24-2015, 12:59 PM
That's a great way. You can also select the membergroupids and then explode them into an array and add the ID you want and implode and insert again as well. You may have more flexibility if you're doing checks and balances with your update.

Google PHP:
implode();
explode();