Home > Article > Backend Development > mysql - PHP randomly inserts a combination of numbers and letters into the database, then determines if it exists in the database, loops it again, and then inserts it into the database.
PHP inserts a random code into the database based on the ID, then determines if the random code already exists in the database and randomly inserts it into the database
PHP inserts a random code into the database based on the ID, then determines if the random code already exists in the database and randomly inserts it into the database
<code>while (true) { // 生成随机数 // 判断数据库中是否存在 if ($noFount) { // 如果不存在 // 插入数据库 break; } }</code>
The field defines a unique key and is inserted using a transaction loop. The insertion is successfully committed and the loop is broken out.
1. First check out all the codes in db
2. Then in_array() until false
3. Insert data