Jump to content

Check array against DB


guitarist

Recommended Posts

Hi all!

 

I'm developing a crude mailing list thingymajig (completely legit, I promise!!).

 

At the moment, I have single email subscription in way of a single field form. The email is entered, checked against the database, and subscribed if it is not there. If it is there, an alert is displayed saying 'email already subscribed'. Great.

 

Now I'm taking things further with a text field and coding hich can extract email addresses from a block of text. No, this isn't so I can harvest email addresses illegitimately, it's because I get asked to add chunks of addresses at a time, and at the moment I have to do this manually, one-by-one.

 

I have the code working to extract the email addresses from the text to an array, but now I'm stuck at the duplication check.

 

I think what I'm asking is: how would it be possible to check each address in the array against the database and highlight any duplicates (I'll ultimately be looking to colour the duplicates and new addresses differently, show the number of new addresses to be added, and have a final 'do it' button which will add them...but I can get there on my own once I learn how to check for dupes!)

 

Hopefully someone can give me a nudge in the right direction :)

 

Many thanks!!

Link to comment
Share on other sites

Not tested, but should give you an idea on approach

 

//Convert array of new emails so original value is key and escaped value is value
$newEmailsAry = array_fill_keys($newEmailsAry, $newEmailsAry);
foreach($newEmailsAry as $key => $value)
{
    $newEmailsAry[$key] = "'" . mysql_real_escape_string($value) "'";
}

//Query to find existing records
$query = "SELECT email FROM table WHERE email IN (" . implode(", ", $newEmailsAry) . ")";
$result = mysql_query($query);

//Create array of existing values
$existingEmails = array();
while($row = mysql_fetch_assoc($result))
{
    $existingEmailsAry[] = $row['email'];
}

//Remove existing values from user entered array
$newEmailsAry = array_diff($newEmailsAry, $existingEmailsAry);

//Create query to insert only new emails
$query = "INSERT INTO table (`email`)  VALUES (" . implode("), (", $newEmailsAry) . ")";
$result = mysql_query($query);

//Ouput results
echo "The following emails already existed in the db<br>\n";
echo "<ul>\n";
foreach($existingEmailsAry as $email)
{
    echo "<li>{$email}</li>\n";
}
echo "</ul>\n";

echo "The following emails were added to the db<br>\n";
echo "<ul>\n";
foreach($newEmailsAry as $email)
{
    echo "<li>{$email}</li>\n";
}
echo "</ul>\n";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.