2
My situation is as follows, I have an email marketing system all in PHP and emails are added per file. CSV, but it is adding duplicate values and blank values.
The problem is that, i have 1 table calls n_emails
with 2 columns, a call ativo
and another email
, the column ativo
has and should have duplicated values, because it will be she who will tell if the email is active or not, IE, in virtually all emails the column value ativo
will be s
. The only part I want the duplicate values to be banned will be in the column email
.
In short, I need to block only duplicate emails and blank emails and at the end, after adding the file. CSV, the system shows the count of added records and added records (in case duplicate emails).
Code that adds the emails is like this:
}
$tabela = "n_emails";
$arquivo = 'addemail/x234gqq.csv';
$arq = fopen($arquivo,'r');
while(!feof($arq))
for($i=0; $i<1; $i++){
if ($conteudo = fgets($arq)){
$ll++; // $ll
$linha = explode(';', $conteudo);//
}
$sql = "INSERT INTO $tabela (ativo, email) VALUES
('$linha[0]', '$linha[1]')";
$result = mysql_query($sql) or die(mysql_error());
$linha = array();
}
echo "Quantidade de Emails Adicionados: ".$ll;
echo "<br><a href='..'>Clique aqui para voltar</a>";
?>
Place the column
email
as key dish in the database.– rray
just rotate
ALTER TABLE n_emails ADD CONSTRAINT UNIQUE (email)
in phpmyadmin itself ?– João Victor Gomes Moreira
In theory yes and this column can not have any duplicated value, otherwise vc will need to remove them before applying the modification.
– rray
It worked, but when loading it shows an error screen and I’d like it to appear for example, Duplicate Emails: {n} Added Emails: {n}, how could I do it @rray ?
– João Victor Gomes Moreira
You will need two counters one for the successful Inserts and the other for the failure ones, use the function
mysql_query()
no if to know if the Insert worked or not, of course it will need a few more adjustments, the basic idea is this.– rray
Instead of doing a disembowelment in the bank to have this behavior, why not change the application to make a
select
before sending theinsert
pro? It is a system with a low volume of data, correct? In this case 2 querys will not affect the performance.– Adriano Luz