如何使用foreach进行多次插入和删除

时间:2014-02-04 06:32:33

标签: php mysql foreach

嗨我是php foreach的新手,我想使用foreach在mysql中循环插入和删除数据。

这是我的代码

<?php function _invite() { 
    $fi = $_POST['fi'];
    if ($_POST['SUPDATE']) {
        // validasi
        // jika valid :
            $fi['nim']=1111,222,333; // from input
            $ex = explode(',',$fi['nim']);

            $sql1 = "DELETE FROM bea_siswaundangan WHERE idbeasiswa='".$_GET['id']."' AND";
            $sql2 = "INSERT INTO bea_siswaundangan (nim,tahun,tgl_input,idbeasiswa) VALUES";
            foreach ($ex as $value) {
             $sql1 .= "nim='".$value."'";
             $sql2 .= "('".$value."','".$fi['tahun']."',now(),'".$_GET['id']."')";
            }
            $sql1=rtrim($sql1, ',');
            $sql2=rtrim($sql2, ',');
            mysql_query($sql1);
            mysql_query($sql2);
            echo'sql1='.$sql1.' dan sql2='.$sql2.'';
            // redirect ke _view -> pake javascript aja
            ?>

            <?php
        // else

            // tampilkan pesan error

    }
    _forminvite($fi);
}?>

输出将是这样的

DELETE FROM bea_siswaundangan WHERE idbeasiswa='1' AND nim='111'nim='222'nim='3333'

但我想要的是

DELETE FROM bea_siswaundangan WHERE idbeasiswa='1' AND nim='1111'
DELETE FROM bea_siswaundangan WHERE idbeasiswa='1' AND nim='2222'
DELETE FROM bea_siswaundangan WHERE idbeasiswa='1' AND nim='1111'

如何做我想要的?

3 个答案:

答案 0 :(得分:1)

将查询的开头放在另一个变量中,然后用$ sql1

连接它
$sql1_begin = "DELETE FROM bea_siswaundangan WHERE idbeasiswa='".$_GET['id']."' AND ";
$sql1 = '';

foreach ($ex as $value) {
    $sql1 .= $sql1_begin."nim='".$value."'; ";
    $sql2 .= "('".$value."','".$fi['tahun']."',now(),'".$_GET['id']."')";
}

修改:要使用一个查询删除,请执行以下操作

$nim = array();
foreach ($ex as $value) {
    $nim[] = "'".$value."'";
    $sql2 .= "('".$value."','".$fi['tahun']."',now(),'".$_GET['id']."')";
}

$sql1 = "DELETE FROM bea_siswaundangan WHERE idbeasiswa='".$_GET['id']."' AND nim IN (".implode(',', $nim).")";

答案 1 :(得分:0)

我已经实现了您的代码并且运行良好     

            foreach ($ex as $value) {

            $sql1 = "DELETE FROM bea_siswaundangan WHERE idbeasiswa='".$_GET['id']."' AND nim='".$value."'";
            $sql2 = "INSERT INTO bea_siswaundangan (nim,tahun,tgl_input,idbeasiswa) VALUES ('".$value."','".$fi['tahun']."',now(),'".$_GET['id']."')";
           }
            mysql_query($sql1);
            mysql_query($sql2);
            echo'sql1='.$sql1.' dan sql2='.$sql2.'';
            ?>

            <?php
    }
    _forminvite($fi);
}?>

答案 2 :(得分:0)

您可以使用implode函数来避免循环。

有点谎言: -

<?php 
function _invite() 
{ 
    $fi = $_POST['fi'];
    $id = intval($_GET['id']);
    if ($_POST['SUPDATE']) 
    {
        // validasi
        // jika valid :
        $fi['nim']=1111,222,333; // from input
        $ex = explode(',',$fi['nim']);

        $sql1 = "DELETE FROM bea_siswaundangan WHERE idbeasiswa=$id AND nim IN ('".implode("','", $ex)."')";
        $sql2 = "INSERT INTO bea_siswaundangan (nim, tahun, tgl_input, idbeasiswa) VALUES ('".implode("','".$fi['tahun']."',NOW(),$id),('", $ex)."','".$fi['tahun']."',NOW(),$id)";
        mysql_query($sql1);
        mysql_query($sql2);
        echo'sql1='.$sql1.' dan sql2='.$sql2.'';
        // redirect ke _view -> pake javascript aja
        // else

        // tampilkan pesan error

    }
    _forminvite($fi);
}
?>

请注意,假设您已清理$ fi ['tahun']