plznty Posted June 6, 2009 Share Posted June 6, 2009 Duplicate entry '[word here]' for key 'username' How can I make it return something like "Username has been taken" instead of a return error of the above. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 6, 2009 Share Posted June 6, 2009 you don't. Database engine errors are that - you have to capture that error and output your own message. Quote Link to comment Share on other sites More sharing options...
plznty Posted June 6, 2009 Author Share Posted June 6, 2009 you don't. Database engine errors are that - you have to capture that error and output your own message. How do I manage that? Thanks.. Quote Link to comment Share on other sites More sharing options...
jxrd Posted June 6, 2009 Share Posted June 6, 2009 Yeah, but then there could actually an sql error; so the user would be told their username is taken no matter what they put. On my site I just run a query to select anyone with the chosen username. If 0 rows are returned; proceed... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 6, 2009 Share Posted June 6, 2009 The most direct solution is to execute the INSERT query, test if it failed or worked, and if it failed with a mysql_errno() value of 1062 (which corresponds to the Duplicate entry xxx for key yyy error), output the custom error message. This only requires some simple conditional logic to be added to test the mysql_errno() in the existing error checking logic that should already be present (everyone already has basic error checking logic if(){}else{} in their application on their queries, right?) The second best way (requires two queries and more php logic) would be to execute a SELECT query to test if the username has already been taken, perform the logic to test how many rows were matched, output the custom error message if there is already a row, then execute the INSERT query if there is not. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.