0

我有一个 CSV 文件上传到与包含我想从数据库中的表(一次全部)中删除的人的电子邮件地址的文件相同的目录中。我使用的代码正确回显,但不会从表中删除任何内容。CSV 文件与包含以下代码的文件上传到同一目录。

<?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!";
    ?>

第 2 轮 - 尝试使用 .txt 文件

<?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!";
    ?>

我仍然收到成功回声,但是为foreach().

4

2 回答 2

1

我最终没有使用上传删除方法,而是创建了一个带有复选框的 html 页面以手动删除。最后,这是最好的方法,因为我需要将一些记录留在一个表中而不是另一个表中。

          <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>
于 2012-05-20T18:50:46.513 回答
0

对于电子邮件地址的 CSV 列表,请尝试以下查询大小:

PHP

$emails = 'a@b.com,b@c.org,foo@example.org';

SQL

$emails = mysql_real_escape_string($emails);
$sql = "DELETE FROM vols2012 WHERE `email` IN ({$emails})";
mysql_query($sql);
于 2012-05-12T03:44:01.257 回答