Home  >  Article  >  Backend Development  >  How to prohibit repeated registration of user names in php

How to prohibit repeated registration of user names in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-17 10:19:542923browse

How to prohibit repeated registration of user names in php: First, when creating the database table, set the field uniqueness for the user nickname; 2. Use the nickname passed in from the front end as the query condition to query the database to see if it can be queried The result, if possible, is that there is duplication.

How to prohibit repeated registration of user names in php

php method to prohibit repeated registration of user names:

1. When creating a database table, for the user Nickname (the name below represents the user nickname field) sets the uniqueness of the field. In this way, subsequent writing operations with the same nickname will fail.

CREATE TABLE `t_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`nickname` varchar(18) NOT NULL unique,
PRIMARY KEY (`Id`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

2. If this requirement is discovered after creating the table, execute the following code to set uniqueness for the nickname field.

ALTER TABLE `user` ADD unique(`nickname`);

3. Use the nickname passed in from the front end as the query condition to query the database to see if the results can be found. If it can, it means there are duplicates, but I am worried that the third method will be used when the database is relatively large. There will be performance issues.

//使用tp框架的代码片段
if(isset($_POST['nickname']))
{
$result=Db::table('user')->field('id')->where('nickname','夏尔')->find();
return empty($result)?'该昵称可以使用':'该昵称已被注册';         
}

The above are some of the methods I know about preventing duplication of user nicknames. The actual method I use is that when the user submits his user nickname, I will check the database. If there is a duplication, I will It is returned to the front end to facilitate the front end to prompt the user in time. Of course, I also set the nickname field as a unique field, multiple guarantees!

Related learning recommendations: php programming (video)

The above is the detailed content of How to prohibit repeated registration of user names in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn