0

我目前有一个表格,列出了我页面上数据库中的所有“产品”。每个产品/列表旁边都有一个复选框,因此您可以一次删除多个产品。我的删除功能与复选框配合得很好,从数据库中删除了选定的产品,但我似乎无法让后半部分工作,这是需要进入名为“gallery”的服务器目录的部分和删除属于已删除产品的图像。

当有人单击删除按钮时,它会运行以下代码:

   //if form was submitted
   if ($submit && $submit == "Delete") {
        //escaping all of them for a MySQL query using array_map
        array_map ('mysql_real_escape_string', $allCheckBoxId);
        //implode will concatenate array values into a string divided by commas
        $ids = implode(",", $allCheckBoxId);
        //building query
        $deleteQuery = "DELETE FROM products WHERE `id` IN ($ids)";
        //running query
        mysql_query($deleteQuery);


            echo $ids;


        //BELOW IS THE PART THAT WILL NOT WORK
        //================================================

        //building query
        $deleteImgQuery = "SELECT * FROM products WHERE `id` IN ($ids)";


        while ($deleteImage = mysql_fetch_array($deleteImgQuery)) {

            $image_delete_id = $deleteImage['id'];
            $image_delete_image = $deleteImage['image'];


            $file= "../gallery/" . $image_delete_id . "/" . $image_delete_image;

            unlink($file);

            echo $file;

        }

   }

它甚至不会执行“echo $file”部分,这让我认为那部分代码根本没有运行。有什么帮助吗?

交换后更新的代码:

   //if form was submitted
   if ($submit && $submit == "Delete") {


        //escaping all of them for a MySQL query using array_map
        array_map ('mysql_real_escape_string', $allCheckBoxId);
        //implode will concatenate array values into a string divided by commas
        $ids = implode(",", $allCheckBoxId);


        //building query
        $deleteImgQuery = "SELECT * FROM products WHERE `id` IN ($ids)";


        while ($deleteImage = mysql_fetch_array($deleteImgQuery)) {

            $image_delete_id = $deleteImage['id'];
            $image_delete_image = $deleteImage['image'];


            $file= "../gallery/" . $image_delete_id . "/" . $image_delete_image;

            unlink($file);

            echo $file;

        }


        //building query
        $deleteQuery = "DELETE FROM products WHERE `id` IN ($ids)";
        //running query
        mysql_query($deleteQuery);


        echo $ids;


   }
4

2 回答 2

2

您需要交换第一部分和第二部分。您正在尝试选择已删除的数据。

于 2012-02-16T16:30:12.830 回答
1

在传递字符串时,您需要执行 $deleteImgQuery 和 mysql_fetch_array 获取资源作为参数。更改以下行..

//building query
$deleteImgQuery = "SELECT * FROM products WHERE `id` IN ($ids)";
$res = mysql_query($deleteImgQuery);

while ($deleteImage = mysql_fetch_array($res)) {
// do
}
于 2012-02-16T16:45:46.023 回答