I’m using php and mysql to develop an application; A little background data about it,it’s a login system which stores usernames and passwords and all the works that a login system does. However I have ran into a problem when it comes to deleting users, the function for deleting users works fine but the problem is For example the administrator can delete himself leaving the database with no user. So what I would want to do is when I am on the main page, a function runs to check if a specific username exists
//Example:
Table name: users
columns: username, password
default username and password I want to always remain in table 'users'
username=dames and password=:password
//End of Example
and if not add the username and password and also if the default username and password has been updated it inserts the default username and password, and and not deleting the updated one.
I hope I made it clear enough because id really appreciate some help and suggestions.Thanks
If you want to make sure that at least one administrator is always present, only delete the administrator if another one is still present:
The given query above expects a table with a username (name) and an access level (access). It matches the user “current-user”, but only if there is another user with the access level “admin”. If “current-user” itself has not access level “admin”, it is matched anyway.
This way, you just need to create one user with “admin” access level once. More users can be added and deleted, just not the very last user with “admin” access level.
If you want to do everything in SQL, you can create a BEFORE DELETE TRIGGER. There, you simply check for the usernames you do not want to be deleted. See How to abort INSERT operation in MySql trigger? for an example – it is given for INSERT, but works the same for DELETE.
Then again, it really depends on your setup if you want to use configuration files (as suggested) or database logic to customise your application.