Home  >  Q&A  >  body text

How to use UNIQUE of PHP and MySQL to ensure that the user name is unique like JD.com registration?

I think that such a quick way to determine the user name duplication is to set the user name field UNIQUE, and then write to catch the exception.
But I tried it and found that the actual operation is not that simple. If the user name is required to be unique, just write the table to see the exception. If it is repeated, it can return false, but if it is successful, the written data will be incomplete. We What is really needed is to write the registration information submitted by the user to the table together. I thought about it and realized that this operation can only be achieved through transactions. As long as the transaction is not committed, the table will not be actually written, but we have already learned that the table Is this piece of data conflicting? I don’t know if my understanding is correct. Or it is achieved through other means. Isn’t it appropriate to judge by select query for a large amount of data?

phpcn_u1582phpcn_u15822640 days ago757

reply all(3)I'll reply

  • 黄舟

    黄舟2017-06-28 09:25:08

    Isn’t it generally true that ajax will do the query after the front-end input is completed? When submitting, check it again before inserting it into the library. It is safer to be unique.

    reply
    0
  • 为情所困

    为情所困2017-06-28 09:25:08

    According to the name entered by the user, check the database and it will be ok. If there is a result, it will prompt that it is occupied. If there is no result, it can be used. If someone submits it first and then prompts that it is occupied while entering other information, it will be ok. Please consider your words. Yes, the probability of this is very low and it is acceptable

    reply
    0
  • 学习ing

    学习ing2017-06-28 09:25:08

    Check it in the database

    reply
    0
  • Cancelreply