Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:59:01+00:00 2026-05-16T05:59:01+00:00

My table is having four fields in which two fields are unique. They are

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:59:02+00:00Added an answer on May 16, 2026 at 5:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a table which has about 17 fields. I need to perform
I want one record from a table having unique Primary Key and duplicate Foreign
I am having four column in my table. When we click one of the
I have DB2 table having following structure CREATE TABLE DUMMY ( ID CHARACTER(10) NOT
I have one table having ID and other attributes. How can I get list
I have a table having 2 columns EmployeeId (int) and EmployeeDetails(XMl type) EmployeeId EmployeeDetails
What I want is to have a table having a first column with every
Is it possible to create a table having column but column without datatype? Meaning
I want many values to be simultaneously inserted in my table having only 2
I want to print some objects in a table having 2 rows per object,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.