views:

35

answers:

3

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.

A: 

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

Amit Ranjan
I had mentioned that i want to use only one query and not more than that. I just wanted to find it with the error string. so that is the reason for me to post a question. so as to find another favorable answer. anyway thank you for taking interest for responding.
Jayapal Chandran
http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/ check out the link, it might help you.
Amit Ranjan
+1  A: 

You can do a strpos() on the errorstring with the actual value ([email protected] 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.

Dennis Haarbrink
yeah, i can also do a strops for the input field instead of the field string. ok. Thank you.
Jayapal Chandran
Besides, i thought of creating a trigger and to throw a custom error message so that using strops to find which field is duplicate.in the trigger error i can display like username already exists or email already exists and i can display this text directly in front end but mysql does not support throwing an error in trigger i think.do you have any suggestion for this?
Jayapal Chandran
A: 

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.

Tomasz Struczyński
Yes the last para of yours is my default condition to check whether an entry already exists. your description is useful and i will keep in mind.
Jayapal Chandran