0

I tried searching for a solution in this website but I can't seem to find any.

I needed to find a way to know which field in my table is causing the constraint error.. In my table, say members table two fields (username,email) are unique keys and defined as unique keys apart from my primary key (memberid). When i add data to this table for a similar user and/or email address this error occurs:

"duplicate entry '[email protected]' for key2"

i want to tell the user which field is causing duplicate entry so as to say "that email is already registered"

I cannot seem to find a mysql function to loop through error keys upon insert...

1
  • This is why I don't usually like using constraints in MySQL. I mean, your app has to know about them, anyway. Commented Nov 9, 2009 at 1:54

2 Answers 2

2

On my sites, I actually check the unique fields myself with Select 1 from tab where field = 'value' limit 1; just so that I can give nice error messages instead of encountering an exception coming back from my insert statement.

Sign up to request clarification or add additional context in comments.

Comments

0

i guess naming constraint fields is not yet a feature in mysql. hope they consider this as this is very useful and saves a lot of code times and lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.