Note that there are some explanatory texts on larger screens.

plurals
  1. POunlink() to delete images from server along with SQL delete
    text
    copied!<p>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. <br/></p> <p>When someone clicks the delete button, it runs this code:</p> <pre><code> //if form was submitted if ($submit &amp;&amp; $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; } } </code></pre> <p>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?</p> <p>UPDATED CODE AFTER SWAP:</p> <pre><code> //if form was submitted if ($submit &amp;&amp; $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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload