harkly Posted January 30, 2010 Share Posted January 30, 2010 I am trying to verify if the email is in the correct format and then if it is already in the database. I am using Ajax to do this but think the issue is on the php end. Right now everything seems to be working but the checking the database, whatever I enter comes out as though it the email is not in the database. Can someone check my code and see what I am doing wrong? <?php // This is a sample code in case you wish to check the userID from a mysql db table include('library/login.php'); login(); mysql_select_db('test'); $email = $_POST['email']; $sql_check = mysql_query("select email from user where email='".$email."'") or die(mysql_error()); if(isSet($_POST['email'])) { if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email)) { echo "<font color=red> Invalid email</font> \n"; } elseif (mysql_num_rows($sql_check)) { echo "<font color='red'>The e-mail ".$email." is already in use.</font> \n"; } else { echo "OK"; } } ?> Link to comment https://forums.phpfreaks.com/topic/190373-verify-emails-format-and-if-already-used/ Share on other sites More sharing options...
ToonMariner Posted January 30, 2010 Share Posted January 30, 2010 save your self a query!!!! check the format 1st - if its OK THEN do the query... If you are using the php >= 5.2 the you could do this... ?php // This is a sample code in case you wish to check the userID from a mysql db table include('library/login.php'); login(); mysql_select_db('test'); $email = isset($_POST['email']) ? $_POST['email'] : NULL; if ( !is_null ( $email ) ) { if (!filter_var($email, FILTER_VALIDATE_EMAIL))) { echo "<font color=red> Invalid email</font> \n"; } else { $sql_check = mysql_query("select email from user where email='".$email."'") or die(mysql_error()); if (mysql_num_rows($sql_check)) { echo "<font color='red'>The e-mail ".$email." is already in use.</font> \n"; } else { echo "OK"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/190373-verify-emails-format-and-if-already-used/#findComment-1004357 Share on other sites More sharing options...
harkly Posted January 31, 2010 Author Share Posted January 31, 2010 Thanks! I am fairly new to php and did not realize that I could check it with php Link to comment https://forums.phpfreaks.com/topic/190373-verify-emails-format-and-if-already-used/#findComment-1004640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.