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

发布于 2025-01-06 02:12:30 字数 2294 浏览 0 评论 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;


   }

I currently have a table listing all of the "products" that are in the database on my page. There is a checkbox next to each product/listing so you can delete multiple products at once.
I have the delete function working well with the checkboxes, deleting the selected products from the database, but I can't seem to get the second half to work, which is the part that needs to go into the server directory named "gallery" and deleting the image that belongs to the products that have been deleted.

When someone clicks the delete button, it runs this code:

   //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;

        }

   }

It won't even do the "echo $file" part which makes me think that that part of the code isnt running at all. Any help?

UPDATED CODE AFTER SWAP:

   //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;


   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2025-01-13 02:12:30

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

You need to swap first and second part. You're trying to select already deleted data.

怎言笑 2025-01-13 02:12:30

您需要在传递字符串时执行 $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
}

You need to execute the $deleteImgQuery and mysql_fetch_array get resource as parameter while you are passing string. Change the following lines..

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

while ($deleteImage = mysql_fetch_array($res)) {
// do
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文