Question

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;


   }
Was it helpful?

Solution

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
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top