0

Hi,

I'm transferring data from one table to another and one of the fields is an email field. I would like to check if the emailfield is empty or otherwise faulty and skip that record if it is.

This is how it looks at the moment but it's problamatic since some emails are not filled out or not correct format (spaces etc):

try {
  // Define and perform the SQL SELECT query
  $sqlish = "SELECT * FROM account WHERE application_type = 'NEW' and DATE(date) > DATE_SUB(CURDATE(), INTERVAL 13 DAY)";
  $resultish = $DBH->prepare($sqlish); 
  $resultish->execute(); 
  $number_of_rows = $result->fetchColumn();

  // If the SQL query is succesfully performed ($result not false)
  if($resultish !== false) {

    // Parse the result set
    foreach($resultish as $rowish) {
      $dbfirstname = $rowish['first_name'];
      $dblastname = $rowish['last_name'];
      $dbemail = $rowish['email_main']; 
      $dbunikid = $rowish['unikid'];

   $resultsbla = $DBH->prepare("insert INTO lime_tokens_262697 (firstname, lastname, email, attribute_1, emailstatus) VALUES ('$dbemail', '$dbemail', '$dbemail', '$dbunikid', 'OK')");
    $resultsbla->execute();


  }
 }
}
catch(PDOException $e) {
  echo $e->getMessage();
}

How would I change this query to also "validate" that the email looks correct and if so inserts it into specified table?

Cheers!
/Adam

2
Contributors
4
Replies
21
Views
4 Years
Discussion Span
Last Post by adishardis
0
function  checkEmail($email) 
{
     if (!preg_match("/^([a-zA-Z0-9\._-])+([a-zA-Z0-9\._-] )*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email)) 
     {

          return false;
     }
     return true;
}

 $dbemail="abc.com";
 if(checkEmail($dbemail))
 {
    echo "ok";
 }
 else
 {
   echo "invalid email";
 }
0

Thanks, one question though since I can't test it right now.

Should I insert the "insert query" instead of the "echo ok" or how would I go about to make it skip the insert if the email is faulty?

Peace
/adam

1
.
.
.
.
if(checkEmail($dbemail))
{
    $resultsbla = $DBH->prepare("insert INTO lime_tokens_262697 (firstname, lastname, email, attribute_1, emailstatus) VALUES ('$dbemail', '$dbemail', '$dbemail', '$dbunikid', 'OK')");
    $resultsbla->execute();
 }
 .
 .
 .
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.