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 6154155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:12:57+00:00 2026-05-23T20:12:57+00:00

I want have an insert query, but before inserting I check whether the username

  • 0

I want have an insert query, but before inserting I check whether the username and email are used by someone else. If used, I want to cancel insert query and echo a message to say whether username or email is in use.

Here my code:

$sql = "SELECT 1 FROM user WHERE username='".$_POST['username']."'";
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result))
    die('This username is already exists');
$sql = "SELECT 2 FROM user WHERE email='".$_POST['email']."'";
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result))
    die('This email address is already exists');
$sql = "insert into user (username,email,password,tel,type) values ('".$_POST['username']."','".$_POST['email']."','".$_POST['password']."','".$_POST['telnumber']."','member')";
if(!mysql_query($sql))
    die(mysql_error());

I want these three sql statements in one. It can be either using cases or something else that you suggest. So,
Is it possible to zip this code into one sql query?

As a result what I need is

sql = "sql_query"
if(!$result = mysql_query($sql))
    die(mysql_error());
while($row = mysql_fetch_array($result)){
if($row['result']==1)
    die('This username is already exists');
else if($row['result']==2)
    die('This email is already exists');
}
die('you have succesfully registered');

thanks for any advice.

  • 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-23T20:12:57+00:00Added an answer on May 23, 2026 at 8:12 pm

    While I suggest you follow @cularis’ answer, you may be interested in the following alternative:

    1. Give email and username the UNIQUE constraint, by creating a unique index for both of these.
    2. run your INSERT query, and if this fails… (due to duplicate keys)
    3. run the suggested combined SELECT, to determine which field existed (username or email)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that returns one row. However, I want to invert the
I want to have an 'updateinfo' table in order to record every update/insert/delete operations
I have a vector that I want to insert into a set . This
I have a page and I want to insert 'Some text' on that page,
I have a custom control and I want to dynamically insert a link to
I have a std::deque, and I want to insert an element at a specified
I have a test file that contains 1,2,3 2,3,4 5,6,7 I want to insert
I think I have seen this question before but I don't think it's answered
I have lists of about 20,000 items that I want to insert into a
I don't know if this question has been ask before. But I have a

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.