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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:22:59+00:00 2026-06-06T20:22:59+00:00

I am making a registration page. I’m worrying about concurrency issues where two users

  • 0

I am making a registration page. I’m worrying about concurrency issues where two users register with the same username at the same time (know it’s real rare, but hate having small flaws in code).

So my approach is, check if the username exists, and if not, insert a new row. I’m using PDO

What I’ve tried

I’m using transactions, but from my question here How exactly do transactions with PHP PDO work with concurrency? it appears that two transactions can read at the same time

  • I don’t think I can use select...for update because I am not updating; in fact, I need to lock an “imaginary” row where a new entry will be added, if it does not exist already
  • I’ve tried googling some examples, but they don’t seem to handle the concurrency issue mentioned above http://php.about.com/od/finishedphp1/ss/php_login_code_2.htm

Solution?

I’ve googled and added a UNIQUE constraint on the username field, but do not want to rely on a MySQL error to rollback the transaction. I’m no expert, but it just doesn’t feel elegant to me. So is there a way to insert if not exists with pure MySQL?

Now, if this really is the way to go, I’ve got a couple questions. If a warning is thrown, does that stop the queries? Like will it throw the exception shown in the example here PHP + MySQL transactions examples ? Or will only a downright-error throw the exception?

Also, it seems to imply here Can I detect and handle MySQL Warnings with PHP? that warnings will show up somehow in the PHP output, but I’ve never had “visible MySQL errors.” The question was not using PDO like in my code, but I was just wondering. Do MySQL errors and warnings make html output? Or does it only say something if I callerrorInfo()?

Thanks

  • 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-06-06T20:23:03+00:00Added an answer on June 6, 2026 at 8:23 pm

    UPDATE

    If I had two unique fields, and did not want to allow either one to be null (since I know I could set one table to “null-able” and have two queries), is there a way to check which one messed up? Like I want to let the user know if username or email was taken. If I do a single insert, I won’t know which one failed

    And furthermore, if I have two statements (first insert username, check if failed, then try same for email), I can only detect one error at a time; if the username fails and rolls back, I cannot try to do the same for the email (well it would be redundantish, because even if the email exists I would have to check to see if the first query had failed) Edit: Think it can work if I use nested try…catch statements

    I think youre over thinking this. You can show a different error to the user for each one but you can really only detect one at a time because mysql is only going to give you one error message with the first problem it encounters. Assuming the previous query (to insert all values at once):

    try {
      $stmt = $db->prepare($sql);
      $stmt->execute(array(
        ':username' => $username,
        ':email' => $email,
        ':password' => $password
      ));
    
      $db->commit();
    } catch (PDOException $e) {
      $db->rollBack();
      if($e->getCode() == 23000) {
    
        // lets parse the message
    
       if(false !== strpos('email', $e->getMessage())) {
           echo 'Email "'.  $email . '" already exists';
        } else if(false !== strpos('username', $e->getMessage()) {
           echo 'Username "'. $username .'" taken';
        }
    
      } else {
         // not a dupe key rethrow error
         throw $e;
      }
    }
    

    Now the hitch with that is the error message is going to look something like:

    Duplicate entry 'THE_VALUE_YOU_TRIED_TO_INSERT' for key THE_KEY_NAME

    The way to get more meaningful reporting on which column it was is to name the indexes with something meanigful when you create the table for example:

    CREATE TABLE the_table (
       `id` integer UNSIGNED NOT NULL AUTO_INCREMENT
       `username` varchar(30),
       `email` varchar(100),
       `password` varchar(32),
       PRIMARY KEY (`id`),
       UNIQUE KEY `uk_email` (`email`),
       UNIQUE KEY `uk_username` (`username`)
    ); 
    

    so now your error message will look like:

    Duplicate entry 'THE_VALUE_YOU_TRIED_TO_INSERT' for key uk_username

    OR

    Duplicate entry 'THE_VALUE_YOU_TRIED_TO_INSERT' for key uk_email

    Which is easily parsable.

    The only real alternative to doing it this way is to do a select on table before you insert to ensure the values dont already exist.


    If youre using PDO you shouldnt have PHP warnings… Just exceptions, which if uncaught will generate a standard php error. IF you have display_errors turned off then that wont be output to the screen, only to the error log.

    I dont think there is a way to insert if not exists so you are back to using a unique key and then catching the exception:

    $db = new PDO($dsn, $user, $pass);
    $sql = "INSERT INTO the_table (username, email, etc) VALUES (:username,:email,:password)";
    
    $db->beginTransaction();
    try {
      $stmt = $db->prepare($sql);
      $stmt->execute(array(
        ':username' => $username,
        ':email' => $email,
        ':password' => $password
      ));
    
      $db->commit();
    } catch (PDOException $e) {
      $db->rollBack();
      if($e->getCode() == 23000) {
        // do something to notify username is taken
      } else {
         // not a dupe key rethrow error
         throw $e;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a site that depend on users to register to be able
I'm making a registration form for my users and I'd like them to write
i'm making a country list for my registration page, but i'm wondering how to
I am making a registration form. When a user is selecting a username I
I'm currently making a registration page for a site and need to show a
I am new to iPad developer, I am making Registration form in my iPad,
So I'm learning PHP with MYSQL right now. Making a user registration and mail
I am making use of django-registration and django-profile to handle registration and profiles. I
I have made a registration program. Making use of mysql database. Can I still
Making a word document of our network set-up. We have about 7 servers and

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.