I have a CSV file uploaded to the same directory as the file containing the email addresses of people I want to delete from a table (all at once) in my database. The code I’m using echoes correctly but doesn’t delete anything from the table. The CSV file is uploaded to the same directory as the file containing the code below.
<?php
// Make a MySQL Connection
mysql_connect("x", "x", "x") or die(mysql_error());
mysql_select_db("volunteers2012") or die(mysql_error());
mysql_query($sql);
/********************************/
$addresses = file('deleteEmails.csv');
foreach($addresses as $address) {
$safe_address = mysql_real_escape_string($address);
$sql = "DELETE FROM vols2012 WHERE `email` = '$safe_address'";
$result = mysql_query($sql) or die(mysql_error());
}
echo "DELETION A GREAT SUCCESS!";
?>
Round 2- Trying with a .txt file
<?php
// Make a MySQL Connection
mysql_connect("x", "x", "x") or die(mysql_error());
mysql_select_db("volunteers2012") or die(mysql_error());
mysql_query($sql);
/********************************/
$addresses = "LOAD DATA LOCAL INFILE ('deleteEmails.txt')";
foreach($addresses as $address) {
$safe_address = mysql_real_escape_string($address);
$sql = "DELETE FROM vols2012 WHERE (email = '$safe_address')";
$result = mysql_query($sql) or die(mysql_error());
}
echo "DELETION A GREAT SUCCESS!";
?>
I’m still getting the success echo, but with the error- Invalid argument supplied for foreach().
I wound up not using the upload to delete method and instead created an html page w/ checkboxes to delete by hand. In the end this was the best method as I needed to leave some of the records in one table but not the other.