-1

I want to integrate FILTER_VALIDATE_EMAIL filter when users want to sign up.

I have this input:

<input type="text" name="email" maxlength="255" />

Some parts of code:

  • $email = strip_tags(mysql_real_escape_string($_POST['email']));
  • $email2 = mysql_query("SELECT * FROM users WHERE email = '{$email}'");

and this:

if(mysql_num_rows($email2) == 1)
{
echo "This email address is already in used.";
}

If you need more code let me know. Thanks!
It's first time when I use MySQL, be good!
Or other way to validate email, ofc.

Mama Tata
  • 3
  • 3

3 Answers3

0

An example how to use:

$email = "someone@example.com";

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
  echo "E-mail is not valid";//Handle not valid email
}else{
  $result = mysql_query("SELECT * FROM users WHERE email = '{$email}'");
  //handle $result of DB
}

From Manual

As side Note: Mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

A useful link Why shouldn't I use mysql_* functions in PHP

Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0

Try following codes:

$email = strip_tags(mysql_real_escape_string($_POST['email']));

if(filter_var($email, FILTER_VALIDATE_EMAIL)) { // Email is valid
    $result = mysql_query("SELECT * FROM users WHERE email = '{$email}'");

   if(mysql_num_rows($result)) {
      echo "This email address is already in used.";
    }
} else {
    echo "E-mail is not valid";
}
Bora
  • 10,529
  • 5
  • 43
  • 73
  • it's necessary use strip_tag if we are using FILTER_VALIDATE_EMAIL for the email? – Emilio Gort Sep 02 '13 at 18:15
  • No, it's not. I guess he use just for more security. But i dont know that `FILTER_VALIDATE_EMAIL` breakable function or not. – Bora Sep 02 '13 at 18:18
0
$email_a = 'mail_check@hotmail.com';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (email_a) email address is considered valid."; 
   //reported as valid
} else {<br />
    echo "This email is invalid";
}

This is an example of filter_var function with the specified filter(FILTER_VALIDATE_EMAIL) usage..

Alexandru
  • 117
  • 1
  • 4
  • 19