PHP和MySQL——检查数据库条目是否不存在?

时间:2022-06-01 17:54:41

I'm collecting the birthdates of users in my system, doing so by linking the the user's unique ID to their birthday entry in another table.

我正在收集我的系统中用户的生日日期,通过将用户的唯一ID与另一个表中用户的生日条目相关联来实现这一点。

To prevent users from accidentally/purposefully entering two birthdate entries for their accounts, I'd like to remove to the entry form for birthdays IF the user has already entered a birthday prior.

为了防止用户意外/故意地为他们的帐户输入两个生日条目,如果用户已经提前输入了生日,我想将其删除到生日条目表单中。

For instance:

例如:

$value = mysqli_query("SELECT bd_user_id FROM user_birthdate WHERE
bd_user_id="$user_id";");

From that data, how will I be able to return some form value to determine if whether the user's ID has already been index in user_birthdate or not? (Where $user_id = The current user's ID)

从该数据中,我将如何返回一些表单值,以确定用户的ID是否已经成为user_birthdate中的索引?(其中$user_id =当前用户的ID)

Or perhaps I'm taking the wrong approach here? The logic behind it is what's been getting me.

或者我在这里采用了错误的方法?这背后的逻辑是我一直想要的。

How can I check if whether a value is NOT indexed in a database table?

如何检查数据库表中的值是否被索引?

1 个解决方案

#1


2  

You normally query the database as you did

您通常像这样查询数据库

$value = mysqli_query("SELECT bd_user_id FROM user_birthdate WHERE bd_user_id="$user_id";");

Than you use mysqli_num_rows(), and check if it returns 0.

然后使用sqmyli_num_rows()检查它是否返回0。

$num_rows = mysqli_num_rows($value);
if($num_rows > 0){
    //exists
}else{
    //doesn't exist
}

**Sorry, as Devon said in your case it's mysqli_num_rows not mysql_num_rows.

抱歉,正如德文在你的例子中所说,它是mysqli_num_rows而不是mysql_num_rows。

#1


2  

You normally query the database as you did

您通常像这样查询数据库

$value = mysqli_query("SELECT bd_user_id FROM user_birthdate WHERE bd_user_id="$user_id";");

Than you use mysqli_num_rows(), and check if it returns 0.

然后使用sqmyli_num_rows()检查它是否返回0。

$num_rows = mysqli_num_rows($value);
if($num_rows > 0){
    //exists
}else{
    //doesn't exist
}

**Sorry, as Devon said in your case it's mysqli_num_rows not mysql_num_rows.

抱歉,正如德文在你的例子中所说,它是mysqli_num_rows而不是mysql_num_rows。