Php 检查a数据库中是否存在用户名、电子邮件、twitter帐户?
我有一张登记表,用户可以在其中插入他的联系信息:Php 检查a数据库中是否存在用户名、电子邮件、twitter帐户?,php,mysql,mysqli,Php,Mysql,Mysqli,我有一张登记表,用户可以在其中插入他的联系信息: 1. username 2. email 3. twitter ID 4. cell number 接下来我要做的是检查数据库,如果其中任何一个已经存在,我想向用户显示一条消息,通知他它已经存在 示例: 如果用户插入用户名“jhon”我想显示一条消息,说明“用户名已经存在。” 简单地说,我只是不想显示像“用户名、电子邮件、twitter ID或手机号存在”这样的消息。该消息应该定义已经存在的输入 这是我的密码: 注意:我知道我必须使用m
1. username
2. email
3. twitter ID
4. cell number
接下来我要做的是检查数据库,如果其中任何一个已经存在,我想向用户显示一条消息,通知他它已经存在
示例:
如果用户插入用户名“jhon”
我想显示一条消息,说明“用户名
已经存在。”
简单地说,我只是不想显示像“用户名、电子邮件、twitter ID或手机号存在”这样的消息。该消息应该定义已经存在的输入
这是我的密码:
注意:我知道我必须使用mysqli
而不是mysql
,但我在这里仅使用它作为示例
<?php
$query = mysql_query("SELECT * FROM `members` WHERE `username` = '". $username ."' OR `email` = '". $email ."' OR `twitterID` = '". $twitterID ."' OR `cellNum` = '". $cellNum ."'");
if (mysql_num_rows($query) > 0)
{
echo 'Username or email or twitter ID or cell number already in use.';
}
?>
这不是直截了当的吗?还是我错过了一些显而易见的东西
$query = mysql_query("SELECT * FROM `members` WHERE `username` = '". $username ."' OR `email` = '". $email ."' OR `twitterID` = '". $twitterID ."' OR `cellNum` = '". $cellNum ."'");
$row = mysql_fetch_assoc($query);
if (isset($row['username'])) {
echo 'Username already exists.';
} else if (isset($row['email'])) {
echo 'Email already exists.';
} else if (isset($row['twitterID'])) {
echo 'Twitter account already exists.';
} else if (isset($row['cellNum'])) {
echo 'cellNum account already exists.';
} else {
echo 'OK';
}
你快到了。您只需检查返回的行是否匹配:
if(mysql_num_rows($query)>0){ //we know we have something
$err_out="";
while($r=mysql_fetch_assoc($query)){//Iterate over all rows
if($username===$r["username"]){
$err_out="Username"; //Set specific error message
break;
}else if($twitterID===$r["twitterID"]){
$err_out="Twitter ID";
break;
}else if($cellNum===$r["cellNum"]){
$err_out="Cellphone number";
break;
}else if($email===$r["email"]){
$err_out="Email";
break;
}
} //while
if($err_out===""){ /*something's wrong, sql injection maybe? */ }
else
echo "Error: $err_out already exists";
}//if
最后的代码绝不是注射预防,只是一个健全的检查。它也会在第一场比赛中停止,继续你可以做一些类似的事情
if($err_out!=="") $err_out.=", ";
$err_out.="Twitter ID";
Error: Username, Twitter ID already exists
当然,还要删除break
语句。这将产生类似于
if($err_out!=="") $err_out.=", ";
$err_out.="Twitter ID";
Error: Username, Twitter ID already exists
如果您不介意稍微滥用英语(“存在”用于多个项目)请将其用作您的查询(注意,可能存在sql注入漏洞):
挑选
(用户名类似于“$username”)作为用户名存在,
(类似“$email”的电子邮件)作为电子邮件存在,
(twitterID类似于“$twitterID”)作为twitterID存在,
(cellNum类似于“$cellNum”)作为cellNum存在
哪里
用户名,如“$username”或
类似“$email”或
twitterID类似“$twitterID”或
cellNum类似“$cellNum”
此sql查询将返回“UsernameExists”和“CellExists”等列。这些值将仅为0或1。如果该值已存在,则为1
然后在使用mysqli_query运行查询后,使用以下代码查找已经存在的数据:
<?php
$alreadyExist = array();
// Loop through the entire result set from the database.
while($row = mysql_fetch_assoc($query)) {
// Look through all of the columns in the database.
foreach($row as $key => $value) {
// If we find that one of the columns has 1, then append to the array of columns that already exist.
if ($value == 1) {
$alreadyExist[] = substr($key, 0, -6);
}
}
}
// If we found information that exists in the database, then print out which ones already exist.
if (!empty($alreadyExist)) echo "The following already exist: ". implode($alreadyExist);
?>
现在,完整代码如下所示:
<?php
$username = "username";
$email = 'test@test.com';
$twitterID = "@sometwitterID";
$cell = "123.123.1234";
$sql = "
SELECT
(A.username like '$username') as UsernameExists,
(A.email like '$email') as EmailExists,
(A.twitterID like '$twitterID') AS TwitterIDExists,
(A.cellNum like '$cell') as CellExists
FROM test.testing A
WHERE
A.username like '$username' OR
A.email like '$email' OR
A.twitterID like '$twitterID' OR
A.cellnum like '$cell'
";
$link = mysqli_connect("localhost", "user", "password", "databasename");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, $sql)) {
$alreadyExist = array();
// Loop through the entire result set from the database.
while($row = mysqli_fetch_assoc($result)) {
// Look through all of the columns in the database.
foreach($row as $key => $value) {
// If we find that one of the columns has 1, then append to the array of columns that already exist.
if ($value == 1) {
$alreadyExist[] = substr($key, 0, -6);
}
}
}
mysqli_free_result($result);
} else {
echo mysqli_error($link);
}
mysqli_close($link);
// If we found information that exists in the database, then print out which ones already exist.
if (!empty($alreadyExist))
echo "The following already exist: ". implode($alreadyExist,", ");
else
echo "No Existing Rows!";
?>
有些不合常规,但您尝试了什么?因此您必须选择所有4个字段并检查哪些字段已经存在。另外,请记住,查询的结果可能会有多条记录。是的,我同意。。无论如何,我已经尝试使用if语句通过专用查询来检查每个字段john@mail.org
还有记录Sammy+john@mail.org
您的脚本将显示什么<代码>用户名已经存在。
?我不知道你清理记录的效果如何。如果您可以创建用户名为“John+的用户”john@mail.org然后是的,它会报告“username allready exists”@u_mulder这个答案不是OP想要实现的吗?。。。“多账户类型的事情没有重复吗?”LozCherone主动提出详细的答复,但失败了。没有人要求他这么做,但只要他提出了这个有缺陷的解决方案,我就想知道为什么有人会投反对票。可能选民根本不知道代码是如何工作的。这只会找到存在的第一个,所以如果有2-4个,其他的就不会显示。不确定这是否是OP的问题,但请注意。您只需检查第一条记录,因为每条记录都是匹配的。我得到一个错误:您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以了解使用near'(A.email-LIKE'的正确语法someone@domain.tld“)AS,(A.twitter类似于‘someTwitterID’”,在第2行
中,似乎在AS之后,您丢失了电子邮件。可能是您的复制粘贴问题。