How can I reset a flag to false, in just one statement, only if it is true & if its already true just ignore it? I would also like to do opposite to this. Here is a situation, I want to ban a user only if he/she is active, if he/she is already banned I dont want to do anything. I also want to unban the user if he/she is banned but don’t want to do anything if he/she is already active. Can this be clubbed in just one statement?
Share
No, this has to be two statements (one to set a user to banned, the other to set the user to unbanned), but it does not have to be complicated.
Assuming that you have a flag “$banned”, you would simply set that to
trueif you wanted the user banned, andfalseif the user is not supposed to be banned. It does not matter what that user was before — simply continue on your way, everything should work itself out nicely.With SQL, you might have a
BANNEDcolumn.UPDATEwon’t care if the value is staying the same, so just run the update:All of that said, you may want to force the user to log out and log back in after they have been banned, but that is a matter for another question.