【发布时间】:2011-07-26 14:24:45
【问题描述】:
当用户在我的网站上注册时,我希望能够检查我的 sql 表/记录并查看他们注册时使用的电子邮件和用户名是否已被使用。如果任何字段留空,以下是我用于返回错误的代码:
//Input Validations
if($user_name == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($user_password == '') {
$errmsg_arr[] = 'Username Password missing';
$errflag = true;
}
if($insp_name == '') {
$errmsg_arr[] = 'Inspector Name missing';
$errflag = true;
}
if($insp_email == '') {
$errmsg_arr[] = 'Inspector Email missing';
$errflag = true;
}
if($confirm_password == '') {
$errmsg_arr[] = 'Confirm Password missing';
$errflag = true;
}
if ($user_password != $confirm_password) {
$errmsg_arr[] = 'The password which you have entered do not match';
$errflag = true;
}
//If there are input validations, redirect back to the registration form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: accountinfo.php");
exit();
}
我尝试过这样的方法来检查我的 sql:
$result=mysql_query("SELECT email FROM members WHERE `email` = '$insp_email'" );
$exist = mysql_fetch_row($result);
if ($exist == 1) {
$errmsg_arr[] = 'That email is already registered.';
$errflag = true;
}
但这不起作用。我知道这可能是一个简单的解决方案,我只是看不到它哈哈。我想要的是检查电子邮件和用户名的 sql。如果这两个中的任何一个已经存在于 sql 中,那么我想重定向回注册页面,并显示一条消息“该电子邮件或用户名已在使用中”
谢谢!
好的,我现在可以使用它了,这正是我想要的。但我注意到的另一个问题是,如果在重定向回注册页面时出现错误(空白字段,用户名已在使用中),它不会显示错误或解释用户被送回注册页面的原因。我的那部分代码再次如下:
//If there are input validations, redirect back to the registration form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: accountinfo.php");
exit();
}
【问题讨论】:
标签: php sql select registration