简体   繁体   English

检查数据库MySQL PHP中是否已经存在用户名

[英]Check if username already exists in database MySQL PHP

I have a registration.php page I made and I need to check if a username is already in my database so that I won't have 2 members with the same name... 我有一个我创建的registration.php页面,我需要检查数据库中是否已存在用户名,这样我就不会有2个具有相同名称的成员...

Here is how I am inserting the user info into the database (username, password, date of registration, etc.): 这是我将用户信息插入数据库的方式(用户名,密码,注册日期等):

mysql_query("INSERT INTO UserDb (ID, TimeStamp, UserName, Password) VALUES ('$ipaddress', NOW(), '$user_username', '$user_password')");

How can I check that database "UserDb" under the "UserName" field to make sure the user's variable "$user_username" doesn't already exist? 如何检查“ UserName”字段下的数据库“ UserDb”,以确保用户变量“ $ user_username”不存在?

You can make the UserName a unique field. 您可以将UserName唯一字段。 MySQL will then refuse to insert a new record if the username already exists. 如果用户名已经存在,MySQL将拒绝插入新记录。

Alternatively you can run a query searching for the username. 或者,您可以运行查询以搜索用户名。 If it doesn't exists, insert it. 如果不存在,请插入。 Wrap this into a transaction so you can be sure that after you've searched for a user, an additional new one with the same name was added before you add the new one. 将其包装成一个事务,这样可以确保在搜索用户之后,在添加新用户之前,先添加了另一个具有相同名称的新用户。

You can make the username field in the database a primary key or unique, which guarantees the username uniqueness in the database. 您可以将数据库中的用户名字段设为主键或唯一键,以保证数据库中的用户名唯一。

Then, if you try to insert an already existing username the mysql_query() function will fail, returning FALSE . 然后,如果您尝试插入一个已经存在的用户名, 则mysql_query()函数将失败,返回FALSE

Besides that, you should always query the database for the existence of the username, using a simple select statement: 除此之外,您应该始终使用简单的select语句在数据库中查询用户名的存在:

SELECT username FROM table WHERE username='$php_username_var';

在UserName上创建一个唯一键,如果INSERT错误出现,请检查错误号。

It's best to check for the username first, rather than depending on the database to tell you via error - always better to be explicit rather than have functionality implied. 最好先检查用户名,而不要依赖数据库来通过错误告诉您-始终最好显式而不是隐含功能。

Also, you may need to consider the situation where there are one or more existing records with the same username. 另外,您可能需要考虑存在一个或多个具有相同用户名的现有记录的情况。

For example, if a user signs up for a subscription, cancels after a few months, and then signs up again later. 例如,如果用户注册了订阅,则几个月后取消订阅,然后稍后再次注册。

Sometimes it's best to re-open the account, and other times just to create a new one...In that case it might be ok to have duplicate rows with the same username, so long as only the current one is active, and the rest are for historical reporting purposes. 有时最好重新开设一个帐户,有时只是创建一个新帐户...在这种情况下,只要用户名是活动的,重复行具有相同的用户名就可以了。其余的用于历史报告。

In fact, you might NEED the duplicate rows if the old user instances are referenced in your billing tables, notes, etc. If you deleted the user, it would cause issues with your reporting. 实际上,如果在计费表,注释等中引用了旧的用户实例,则可能需要重复的行。如果删除了该用户,则会导致报告问题。

A word of advice as well - consider using the user's email address as their username - you know it's a string that's unique to them, gives you a default way to contact them, and can be validated at sign up. 也有一些建议-考虑使用用户的电子邮件地址作为用户名-您知道这是他们唯一的字符串,为您提供了一种默认的联系方式,并且可以在注册时进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM