Jump to content

Deleting inside a loop


floridaflatlander

Recommended Posts

I've seen two post now about not deleting within a loop and I think I may need to ask a question about this.

 

I have a photograph db that gives the addresses of the thumbs and photos in files for a product, it also is keyed to the products db. There can be more than one photo per product.

 

If someone selects yes from a radio button and clicks submit the process should delete the record of the product in the product db, delete the photos in the photo images/files(this is the loop) and then delete the product id and row in the photo db

 

Is this a decent way of doing this?

 

 

if ($_POST['sure'] == 'Yes') { // If Yes delete the item and its pictures in image folders

	$q = "DELETE FROM product WHERE id = $item LIMIT 1"; // Delete the product record		
	$r = @mysqli_query ($dbc, $q);

	$q = "SELECT id, thumb, photo FROM photos WHERE id = '$item'";
	$r = @mysqli_query ($dbc, $q);
	if ($r){
		while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
		unlink('images/members/'.$row['thumb'].''); // Remove-delete photos from folders for each row in photo db
		unlink('images/members/'.$row['photo'].'');
		}
	}

	$q = "DELETE FROM photos WHERE id_prod = $item"; // Delete the photo record		
	$r = @mysqli_query ($dbc, $q);

	// When through redirect
	header("Location: $home/member/index.php?user=$mem_id");
	exit();
	}

Link to comment
Share on other sites

Probably your best way is to just delete the whole folder with:

public static function deleteDir($dirPath) {
    if (! is_dir($dirPath)) {
        throw new InvalidArgumentException('$dirPath must be a directory');
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            self::deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}

 

Or something similar, then remake the folder using mkdir();

 

Sorry if this isn't the answer you were looking for, but I didn't understand your question really.

Link to comment
Share on other sites

Don't delete the whole folder, that's obviously not what you need.

 

Your code seems fine, but you should probably delete the item AFTER you delete the images.  That's just me being a paranoid programmer, but I never delete the parent before I delete the children.

 

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.