My table is having four fields in which two fields are unique. They are username and email for example. People register in the front end. I dynamically display the result of their registration using ajax.
I am doing a single insert query. And if there is an error then i use the error number to find whether it is 1062(duplicate entry) or 1064(query string error) else the user has successfully registered.
If the user could not be registered then i display the reason in a user friendly way. If there is duplicates then i have to display username already exists or email already exists.
The point here is i dont want to use a select query to see whether username or email already exists and if not exists the doing an insert query.
If i am doing like the above the there two queries have to be executed. I want to run only one query and with the help of the error number and the error message i want to display the user the type of error exactly.
Now the problem is with the duplicacy.
I found out that it is dupilcate and i have the error string as like this
Duplicate entry '[email protected]' for key 'email' or Duplicate entry 'testing' for key 'username'
i thought of using strpos to find the field name so that i can display the user that either email already exists or username already exists.
but in some servers i get like this
Duplicate entry '[email protected]' for key 2' or Duplicate entry 'testing' for key 3
in this case i need to do a regex to extract the last part and if it is a string i will do a strcmp to fine which field is duplicate and i will display the error to the user accordingly or it is a number then i need a simple switch statement to fine which field has the duplicate.
**
So is there any other way to get which field is duplicate if there are more unique fields in the table.
** Or if there is no then i just want to put this question to peoples mind so that if they have already done a gimmick for this then they could share here so that people like me would be benefit. Thank you.