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

Recommended Answers

All 4 Replies

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";
 }

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

.
.
.
.
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();
 }
 .
 .
 .
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.