Reputation: 561
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()
.
Upvotes: 0
Views: 2114
Reputation: 561
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.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// Delete the customer rows (only if the form has been submitted)
if(isset($_POST['submit']))
{
foreach($_POST['todelete'] as $delete_id) {
$query="DELETE FROM volConfirm WHERE email='$delete_id'";
mysql_query($query) or die ('Error in query: ' .mysql_errno() . mysql_error());
}
}
// Display the customer rows with checkboxes for deleting
$query="SELECT * FROM volConfirm";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
echo '<input type="checkbox" value="' . $row['email'] .'"name="todelete[]" />';
echo $row['agreeName'];
echo ' '.$row['position'];
echo ' '.$row['shift_times'];
echo ' '.$row['email'];
echo ' '.$row['confirmed'];
echo '<br />';
echo '<br />';
}
?>
<input type="submit" name="submit" value="Remove" />
</form>
Upvotes: 1
Reputation: 26124
For a CSV list of email addresses, try this query on for size:
PHP
$emails = '[email protected],[email protected],[email protected]';
SQL
$emails = mysql_real_escape_string($emails);
$sql = "DELETE FROM vols2012 WHERE `email` IN ({$emails})";
mysql_query($sql);
Upvotes: 0