Compare form domain with database domain

1

I need to compare domain submitted and processed by a form through $_POST with database domains because I can only insert an email from each domain in the database. For this first I got the email from $_POST and then stored all the emails from the database in an array with mysql_fetch_array in the database. Now I need to make this array return only the domains as I did with explode which resulted in the $tmpdominio variable and then compare these two information to see if the domain already exists in the database. As I'm starting, so far I have this code.

# Pegando o dominio deste email
$emaildoform    = $_POST['email'];
$tmpdominio     = explode('@', $emaildoform);

# Pegando todos os emails da minha tabela
$dados      = mysql_query("SELECT email FROM wp_agencias");

while($arraydeemails = mysql_fetch_array($dados)){

    # Eu preciso extrair o domínio de cada e-mail do array ...
    # ... depois vou comparar com $tmpdominio para ...
    # ...verificar se dominio está presente na base

};
    
asked by anonymous 17.05.2014 / 16:29

3 answers

3

Maybe the solution meets your need, then you have a good idea of what can be done. Success!

if (isset($_POST['email'])){
   // Recuperando domínio do e-mail recebido pelo form
   $dominio = explode( '@', $_POST['email'] );

   // Pegando todos os emails da minha tabela
   $dados = mysql_query("SELECT email FROM wp_agencias WHERE email LIKE @".$dominio[1]);

if( $dados) {
   echo "Domínio ". $dominio[1]." já cadastrado";
} else {
   echo "Domínio ". $dominio[1]." não cadastrado";
}
}
    
17.05.2014 / 17:16
2
// Extrai apenas o dominio do e-mail.
$dominio = explode('@', $_POST['email']);
$dominio = $dominio[1];

// Torna a string segura para uso em consultas.
// (Evita SQLInjection).
$dominio = mysql_real_escape_string($dominio);

// Consulta se o dominio já existe.
$dados = mysql_query('SELECT email FROM wp_agencias WHERE email LIKE "%@'.$dominio.'"');

if (mysql_num_rows($dados)>0) die("Dominio já existe");
else
{
    // Torna a string segura para uso em consultas.
    $email = mysql_real_escape_string($_POST['email']);

    // Insere no banco de dados.
    mysql_query('INSERT INTO wp_agencias(email) VALUES ("'.$email .'");');
}
    
17.05.2014 / 18:38
1

First, I would like to know if this list of emails is too large. If it is it can give you performance problem. But I'll talk about two options. If your list is small this gist here can help you solve:

<?php
class DomainCheck {
    private $email;

    function __construct($email_a_verificar) {
        $this->email = $email_a_verificar;
    }

    private function getDominio($email) {
        $email = explode('@',$email);
        return $email[ count( $email ) - 1 ];
    }

    function check($lista_de_emails) {
        $dominio_a_verificar = $this->getDominio( $this->email );

        foreach( $lista_de_emails['email'] as $email ) {
            $dominio_atual = $this->getDominio( $email );

            if ( $dominio_a_verificar == $dominio_atual ) {
                throw new Exception("Domínio existente na lista", 1);
            }
        }
    }
}

//a lista simula o seu recordset
//usando um fetchAll do PDO
$lista_emails_do_banco = array(
    'email'=>array(
        '[email protected]',
        '[email protected]',
        '[email protected]',
        '[email protected]',
        '[email protected]'
    )
);

$email_recebido_do_form = '[email protected]';

$check = new DomainCheck($email_recebido_do_form);
try {
    $check->check( $lista_emails_do_banco );
    echo "Sem duplicações de e-mail";
    //Rotina de salvamento do registro pode vir aqui
} catch( Exception $e ) {
    echo $e->getMessage();
}

link

But if your list is soooo big, then you can add a field to the domain in the table with the emails and then search the domain field.

    
17.05.2014 / 17:05