簡體   English   中英

在foreach中使用復選框刪除數據

[英]Delete data with checkboxes in foreach

我用復選框選擇了多行,然后用下面的代碼用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']);   
    }

希望我能理解您的問題。 代碼不會被檢查,但是請嘗試使其適應您的環境。

<?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.");
}
?>

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM