duansen6750 2011-07-19 09:49
浏览 21
已采纳

如何在mysql php中使用IN OPERATOR删除所有行

i am having problem in understanding the behaviour of this programme below is simple code to delete the email address using IN operator

$emails = $_POST['ids'];
    $sql = "DELETE FROM newsletter where email ";
    $condition = sprintf('IN ("%s")',implode(' "," ',$_POST['ids']));
    $sql = $sql.$condition;
    include '../includes/db.php';
    $r = mysql_query($sql);

    echo $sql;

it only deletes one email id and returns true . how can i make it run in a way it deletes all the emails .

below is the query constructed using the above code.

DELETE FROM newsletter where email IN ("adfadsf@gmail.com "," asdfasfasf@gmail.com "," kjhkhsd@assdfsdf.sdfsf "," shit@gshit.com "," someother@gmail.com")

is it wrong way of deleteing ?

  • 写回答

4条回答 默认 最新

  • duanmen2189 2011-07-19 09:51
    关注

    Instead of:

    $condition = sprintf('IN ("%s")',implode(' "," ',$_POST['ids']));
    

    do:

    $condition = sprintf('IN ("%s")',implode('", "',$_POST['ids']));
    

    IN operator matches contents of the field with the values exactly. Spaces at the beginnings and ends of values might have cause your problems.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?