unlink()从服务器删除图像以及SQL删除

时间:2012-02-16 16:26:16

标签: php sql image unlink

我目前有一个表格,列出了我页面上数据库中的所有“产品”。每个产品/商品旁边都有一个复选框,因此您可以一次删除多个商品。 我有删除功能与复选框一起使用,从数据库中删除所选产品,但我似乎无法让下半部分工作,这是需要进入名为“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;


   }

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

while ($deleteImage = mysql_fetch_array($res)) {
// do
}