Question

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 'test@gest.com' 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 'test@gest.com' 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.

Was it helpful?

Solution

First thing first - I see another problem in your query - you mention 1064(query string error). This means that you can have bad query syntax. Which means that you probably don't filter user values enough. This error occurs (AFAIK) ONLY when you have incorrectly secured server. Which will lead to much more problems than using two SQLs instead of one. Check if you escape input parameters correctly. You should also use parameter binding like that:

$stmt = mysqli_prepare($link, "INSERT INTO XXX (username, email) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, 'ss', $user, $email);

More at mysql page http://pl.php.net/manual/en/mysqli-stmt.bind-param.php

And about your bug:

Sorry, but there's no way (at least none I know of) to achieve what you want. Don't be so picky about query number on inserts. It's not like you'll have thousands of user registrations per second so another query won't really hurt.

the problem is, you don't really know what the error message will be. This depends mainly on server configuration. You can have error message in another language etc.

Really, just do SELECT like SELECT username, email FROM database WHERe username=? OR email=? and check the response. If none, no duplicates.

OTHER TIPS

You can do a strpos() on the errorstring with the actual value (some@email.com or some_username)

Edit:
You could also ask yourself if it is really necessary to do this. There are other ways:

  • Tell the user that username and/or email already exists
  • Do a check onblur() on the appropriate input fields

This way you completely avoid the problem altogether.

Just use Select Count(*) From TableName where Condition='yourtestcondition'. If you got count > 0 then it exists otherwise not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top