我有一个注册表单,用户可以在其中插入他的联系信息:
1. username
2. email
3. twitter ID
4. cell number
我接下来要做的是检查数据库,如果其中任何一个已经存在,我想向用户显示一条消息,通知他它已经存在。
示例:
如果用户插入了用户名"jhon"
,我想显示一条消息,说明" username
已经存在。"
简单地说,我只是不想显示像"用户名或电子邮件或推特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.';
}
?>
我希望我很清楚...
任何帮助将不胜感激。
答案 0 :(得分:1)
不是直截了当吗?或者我会错过一些完全明显的东西吗?
$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';
}
答案 1 :(得分:0)
你几乎就在那里。您只需检查返回的行以查找匹配项:
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";
当然会删除break
语句。这会产生类似
Error: Username, Twitter ID already exists
如果您不介意轻微滥用英语(&#34;存在&#34;对于多个项目)
答案 2 :(得分:0)
使用此作为您的查询(请注意可能存在SQL注入漏洞):
SELECT
(username like '$username') as UsernameExists,
(email like '$email') as EmailExists,
(twitterID like '$twitterID') AS TwitterIDExists,
(cellNum like '$cellNum') as CellExists
WHERE
username like '$username' OR
email like '$email' OR
twitterID like '$twittterID' OR
cellNum like '$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!";
?>