Home:ALL Converter>checking multiple email fields in mysql

checking multiple email fields in mysql

Ask Time:2013-01-28T01:45:40         Author:user2014429

Json Formatter

I have a form with multiple 'email' input fields. each is checked in mysql to check if its in the database. what I have now works if an incorrect email is in one of the fields it echo's the right message and if all fields are empty it also echo's the right message. And if the last input field is a correct email it also works. the one problem is that if a correct email address is in one or more of the fields that is not the last one, and there are no other incorrect emails, it will echo "none filled in". I can rectify this by adding a break inside {$x = "1"} but then that messes up the mysql check as it needs to continue until/if the condition is not met. is there anything I can do?

foreach($_POST as $value)
  {
     if(empty($value))
      {
       $x = "0";
      } else
     if(!empty($value))
      {
        $usercheck =  "SELECT email FROM users WHERE email = '$value'";
        $usercheck = $db->query($usercheck);

     if($usercheck->num_rows > 0)
      {
        $x="1";
      } else
        {
          $x="2"; break;
        }
      }
  } 

if($x == "0") {echo "none filled in";}
if($x == "1") {echo "all good";}
if($x == "2") {echo $value." is not a user";} 

Author:user2014429,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14550215/checking-multiple-email-fields-in-mysql
yy