使用foreach中的复选框删除数据

时间:2016-10-20 17:41:45

标签: php mysql

我选中带有复选框的多行,然后用ajax删除它们,代码如下。 (它工作正常..)

    <?php
include_once("../../files/connect.php");
if(isset($_POST['val']))
{
    $p = &$_POST['val'];
    foreach($p as &$v)
    {
        $v = "'". mysqli_escape_string($kapcs, $v) ."'";
    }
    $values = '('.implode(',',$p).')';
    mysqli_query($kapcs, "DELETE FROM kereses WHERE kereses_id IN $values") or die(mysqli_error($kapcs));

    echo 'Selected rows deleted.';
}
else
{
    exit("No rows selected with checkbox.");
}
?>

我如何将此代码放入其中?我想从文件夹中删除图像或文件,而不仅仅是从sql表中删除记录。有人能帮助我吗?

$DestinationDirectory = "../../images/news/";
    if(file_exists($DestinationDirectory.$data['hir_thumb']))
    {
        unlink($DestinationDirectory . $data['hir_thumb']); 
    }
    if(file_exists($DestinationDirectory.$data['hir_big']))
    {
        unlink($DestinationDirectory . $data['hir_big']);   
    }

1 个答案:

答案 0 :(得分:0)

我希望我能理解你的问题。 代码没有检查,但尝试使其适应您的环境。

<?php
include_once("../../files/connect.php");
if(isset($_POST['val']))
{
$p = &$_POST['val'];


foreach($p as &$v)
{
    $v = "'". mysqli_escape_string($kapcs, $v) ."'";
//select all the row

        $sor = $kapcs->query("SELECT * FROM kereses WHERE kereses_value = {$v}");

//fetch the row
    while ($data = mysqli_fetch_row($sor)){

            //delete selected rows
            $delete = $kapcs->query("DELETE FROM kereses WHERE id= {$data['id']}");

            // if delete success
            if ($delete) {

                $DestinationDirectory = "../../images/news/";
                echo 'Selected row deleted from database.';
                    // unlink the thumbnail if exists
                                if(file_exists($DestinationDirectory.$data['hir_thumb']))
                                {
                                    unlink($DestinationDirectory . $data['hir_thumb']); 
                                    echo 'Selected thumbnail deleted from filesystem.';
                                }
                    //unlink the big picture if exists
                                if(file_exists($DestinationDirectory.$data['hir_big']))
                                {
                                    unlink($DestinationDirectory . $data['hir_big']);   
                                    echo 'Selected picture deleted from filesystem.';
                                }
            }


    }
 }


}
else
{
 exit("No rows selected with checkbox.");
}
?>

我希望有所帮助。