When a user registers on my site, I want to be able to check my sql table/records and see if that email and username they are registering with has already been used. Below is the code I use for returning errors if any of the fields are left blank:
//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();
}
I have tried something like this to check my 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;
}
That doesn’t work though. I know it is probably an easy solution, I just can’t see it lol. What I would like is to check the sql for the email and username. If either of those 2 exist already in the sql, then I want to redirect back to the registration page, and display a message saying “That email or username is already in use”
Thanks!
Ok I have it working now and it’s just what I wanted. But another problem I noticed is if there is an error (blank field, username already in use) when it redirects back to the registration page, it doesn’t display the error or explain why the user was sent back to the registration page. Again my code for that part is below:
//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();
}
1 Answer