php - Finding out why a mysqli INSERT failed - which unique index was duplicated? -


i'm writing rest style php server needs allow user registration. basics working focusing on error handling now. users table has unique index on 'user_name' field , 'user_email' field. when try insert duplicate value can read error , string like

duplicate entry 'noggin182' key 'user_name'

i need translate can display user. there easy way more details of why insert failed? parse string find out feels messy, things break if string gets changed in update or language changes.

if you're trying think you're trying do, might worth taking different approach, , putting logic adding user in stored procedure, in single transaction, , doing checks beforehand see if user exists.

you return own logic whether user added, or reason why couldn't be. or raise own error or maybe.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -