如何检查表中是否已存在数据?

时间:2021-09-18 04:26:48

I have a table named users two of it's fields are login and name.
These fields can not be duplicate.
When the user are going to create a new registration, I already made a method to check if that name/login already exists or not.
But the user can also edit his login/name.
When the user enters on the page to edit his registration data, it already fills the fields with the current data.
I have 3 fields [NAME] [EMAIL] [LOGIN].
The user can edit only 1 of this or all of them at once ...
How may I create a method to check if that name/loginalready exists or not when he try to edit it ?
Maybe a Query ?
A select count on the login then on the name field ?

我有一个名为users的表,其中两个字段是login和name。这些字段不能重复。当用户要创建新注册时,我已经创建了一种方法来检查该名称/登录是否已存在。但是用户也可以编辑他的登录名/名称。当用户在页面上输入以编辑他的注册数据时,它已经用当前数据填充了字段。我有3个字段[NAME] [EMAIL] [登录]。用户一次只能编辑其中一个或全部...当我尝试编辑它时,如何创建一个方法来检查该名称/ loginalready是否存在?也许一个查询?登录时选择计数然后在名称字段上?

--UPDATE--
here's my solution

--UPDATE--这是我的解决方案

4 个解决方案

#1


1  

You should leave this up to the database system you are using and handle any errors it may throw. All database systems (Access, Oracle, MS SQL, etc) allow you to mark a table field as being Unique. This means that the table can only hold one records with a field(s) with that value. If you try to add more than one record with a same field, you will be thrown an error. Your application should catch that error and alert the user. If you post what kind of database system you are using I can show you how to do this.

您应该将其保留在您正在使用的数据库系统中并处理它可能引发的任何错误。所有数据库系统(Access,Oracle,MS SQL等)都允许您将表字段标记为唯一。这意味着该表只能包含一个带有该值的字段的记录。如果您尝试使用相同的字段添加多个记录,则会引发错误。您的应用程序应捕获该错误并提醒用户。如果你发布了你正在使用的数据库系统,我可以告诉你如何做到这一点。

Edit:

编辑:

Heres is an example. This uses the SqlClient.SqlException exception class. I'm not sure what the error code is for unique constraints but I added a variable in the catch that you can place a break point on to get. Just change the if statement to match that error code:

赫雷斯就是一个例子。这使用SqlClient.SqlException异常类。我不确定唯一约束的错误代码是什么,但我在catch中添加了一个变量,你可以设置一个断点来获取。只需更改if语句以匹配该错误代码:

    Try
        'your database insert attempt here
    Catch ex As SqlClient.SqlException
        Dim sqlErrorNumber = ex.ErrorCode
        If (sqlErrorNumber = 1) Then
            Me.lblWarning.Text = "Please select a unique ID"
            Me.lblWarning.Visible = True
            Me.lblWarning.ForeColor = Drawing.Color.Red
            Me.lblWarning.Font.Bold = True
        End If

    End Try

In the aspx page:

在aspx页面中:

<asp:Label ID="lblWarning" runat="server" Visible="false"></asp:Label>

#2


0  

What is your primary key here?

你的主要关键是什么?

I suggest, Adding new identity column to your table will solve the issue.

我建议,在表中添加新标识列将解决问题。

You can edit the row based on the identity column.

您可以根据标识列编辑行。

For Ex: Add [UserID] field and made it unique identity column. then perform update operations based on userid in where clause.

对于Ex:添加[UserID]字段并使其成为唯一标识列。然后根据where子句中的userid执行更新操作。

#3


0  

You should use unique fields in your database, try catch in php or asp and ajax technique to get sure that the fields are filled or not with the informations that are you typing now.

您应该在数据库中使用唯一字段,尝试使用php或asp和ajax技术捕获以确保字段已填充或不填充您现在键入的信息。

#4


0  

if you don't have a column ID or if username column is not set like UNIQUE you can use this function :

如果您没有列ID或者如果未将用户名列设置为UNIQUE,则可以使用此功能:

function user_exists($username){
$username = sanitize($username); // sanitize
return(mysql_result(mysql_query("SELECT COUNT(user_name) FROM user_table WHERE username = '$username'"), 0) == 1) ? true : false;
}

function user_exists($ username){$ username = sanitize($ username); //清理返回(mysql_result(mysql_query(“SELECT COUNT(user_name)FROM user_table WHERE username ='$ username'”),0)== 1)?真假; }

For New Members / Or if current User wants to edit his username this function return false if username is not registered and true if it is

对于新成员/或者如果当前用户想要编辑他的用户名,如果未注册用户名,则此函数返回false,如果是,则返回true

#1


1  

You should leave this up to the database system you are using and handle any errors it may throw. All database systems (Access, Oracle, MS SQL, etc) allow you to mark a table field as being Unique. This means that the table can only hold one records with a field(s) with that value. If you try to add more than one record with a same field, you will be thrown an error. Your application should catch that error and alert the user. If you post what kind of database system you are using I can show you how to do this.

您应该将其保留在您正在使用的数据库系统中并处理它可能引发的任何错误。所有数据库系统(Access,Oracle,MS SQL等)都允许您将表字段标记为唯一。这意味着该表只能包含一个带有该值的字段的记录。如果您尝试使用相同的字段添加多个记录,则会引发错误。您的应用程序应捕获该错误并提醒用户。如果你发布了你正在使用的数据库系统,我可以告诉你如何做到这一点。

Edit:

编辑:

Heres is an example. This uses the SqlClient.SqlException exception class. I'm not sure what the error code is for unique constraints but I added a variable in the catch that you can place a break point on to get. Just change the if statement to match that error code:

赫雷斯就是一个例子。这使用SqlClient.SqlException异常类。我不确定唯一约束的错误代码是什么,但我在catch中添加了一个变量,你可以设置一个断点来获取。只需更改if语句以匹配该错误代码:

    Try
        'your database insert attempt here
    Catch ex As SqlClient.SqlException
        Dim sqlErrorNumber = ex.ErrorCode
        If (sqlErrorNumber = 1) Then
            Me.lblWarning.Text = "Please select a unique ID"
            Me.lblWarning.Visible = True
            Me.lblWarning.ForeColor = Drawing.Color.Red
            Me.lblWarning.Font.Bold = True
        End If

    End Try

In the aspx page:

在aspx页面中:

<asp:Label ID="lblWarning" runat="server" Visible="false"></asp:Label>

#2


0  

What is your primary key here?

你的主要关键是什么?

I suggest, Adding new identity column to your table will solve the issue.

我建议,在表中添加新标识列将解决问题。

You can edit the row based on the identity column.

您可以根据标识列编辑行。

For Ex: Add [UserID] field and made it unique identity column. then perform update operations based on userid in where clause.

对于Ex:添加[UserID]字段并使其成为唯一标识列。然后根据where子句中的userid执行更新操作。

#3


0  

You should use unique fields in your database, try catch in php or asp and ajax technique to get sure that the fields are filled or not with the informations that are you typing now.

您应该在数据库中使用唯一字段,尝试使用php或asp和ajax技术捕获以确保字段已填充或不填充您现在键入的信息。

#4


0  

if you don't have a column ID or if username column is not set like UNIQUE you can use this function :

如果您没有列ID或者如果未将用户名列设置为UNIQUE,则可以使用此功能:

function user_exists($username){
$username = sanitize($username); // sanitize
return(mysql_result(mysql_query("SELECT COUNT(user_name) FROM user_table WHERE username = '$username'"), 0) == 1) ? true : false;
}

function user_exists($ username){$ username = sanitize($ username); //清理返回(mysql_result(mysql_query(“SELECT COUNT(user_name)FROM user_table WHERE username ='$ username'”),0)== 1)?真假; }

For New Members / Or if current User wants to edit his username this function return false if username is not registered and true if it is

对于新成员/或者如果当前用户想要编辑他的用户名,如果未注册用户名,则此函数返回false,如果是,则返回true