Jump to content


deleting a product and its images?


5 replies to this topic

#1 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 08 January 2006 - 05:41 PM

Ok I have a function to delete a product and its images from my store, I need to know if this is correct before I use it.

In the datavase the images paths are as /images/items/image.gif, so do I need the path in the unlink command?

function productDelete($id) {
// Delete the product and corresponding images

	// Get the image name and thumbnail and remove it
	$query 	= db_query("SELECT image_large, image_thumb FROM store_product WHERE id = $id");
	$result = db_fetch_object($query);
	unlink("images/items/".$result->image_large);
	unlink("images/items/".$result->image_thumb);
	
	// Remove any references to this product from the cart and orders
	$query 	= db_query("DELETE FROM store_order WHERE id_pro = $id");
	$query 	= db_query("DELETE FROM store_cart WHERE id_pro = $id";	
	
	// Delete the item
	$query 	= db_query("DELETE FROM store_product WHERE id = $id")
}


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 08 January 2006 - 08:42 PM

You can never be sure something is correct until you use it. The best way to find out if it is correct is to back up anything it will modify, then run it and hope for good luck. But i think i know something that could cause a few problems.

Since the image path in your database is already the full path, then you probably shouldn't add the path again in our unlink() functions.
In other words, you might want to change the unlink functions to the following.
	unlink('.'.$result->image_large);
	unlink('.'.$result->image_thumb);


#3 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 08 January 2006 - 09:42 PM

Ya I tried it and it worked, but I revised it so that it wont delete my no-item image.

function productDelete($id) {
// Delete the product and corresponding images

	// Get the image name and thumbnail and remove it
	$query 	= db_query("SELECT image_large, image_thumb FROM store_product WHERE id = $id");
	$result = db_fetch_object($query);
	
	if($result->image_large && $result->image_thumb != "images/items/no-item.gif") {
		unlink("$result->image_large");
		unlink("$result->image_thumb");
	}
	
	// Remove any references to this product from the cart and orders
	$query 	= db_query("DELETE FROM store_order WHERE id_pro = $id");
	$query 	= db_query("DELETE FROM store_cart WHERE id_pro = $id");	
	
	// Delete the item
	$query 	= db_query("DELETE FROM store_product WHERE id = $id");
}


#4 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 08 January 2006 - 10:02 PM

Ok I have a question, when im editing a product it shows its current image. You can either choose to delete the image, it removes it an gives it the no-item image. Also they can upload a new one when checking the checkbox, but a small question.

If they check the thumbnail image only and not the large image wouldnt the number in thumbnail have to be 0 and not 1?

function productEdit($id) {
// Add the product to the store
	$cat		 = $_POST["cat"];
	$name		= $_POST["name"];
	$description = $_POST["description"];
	$price	   = str_replace(',', '', (double)$_POST["price"]);
	$quantity	= (int)$_POST["quantity"];
	$upimage 	 = isset($_POST["upimage"]) ? "on" : "";
	$upthumb 	 = isset($_POST["upthumb"]) ? "on" : "";
	
	// Upload/Edit/Remove images and update using checkboxes
	if ($upimage == "on") {
		// Delete old Image
		$query  = db_query("SELECT image_large FROM store_product WHERE id = $id");
		$result = db_fetch_object($query);
		if ($result->image_large != "images/items/no-item.gif") {
			unlink("$result->image_large"); }
			
		// Upload new one
		$image = "images/items/".$_FILES["image"]["name"][0];
		copy($_FILES["image"]["tmp_name"][0], $image);
		$query  = db_query("INSERT INTO store_product (image_large) VALUES ('$image')"); }
		
	if ($upthumb == "on") {
		// Delete old Image
		$query  = db_query("SELECT image_thumb FROM store_product WHERE id = $id");
		$result = db_fetch_object($query);
		if ($result->image_thumb != "images/items/no-item.gif") {
			unlink("$result->image_thumb"); }
			
		// Upload new one
		$thumb = "images/items/".$_FILES["image"]["name"][1];
		copy($_FILES["image"]["tmp_name"][1], $thumb);
		$query  = db_query("INSERT INTO store_product (image_thumb) VALUES ('$thumb')"); }
	
	// Update settings
	$query  = "UPDATE store_product SET id_cat = '$cat', name = '$name', description = '$description', 
				price = '$price', quantity = '$quantity' WHERE id = '$id'";
	$result = db_query($query);	
}


#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 09 January 2006 - 09:17 PM

Yep, you're right. But the good news is theres 2 ways to fix that.
1. Make the script more complex by adding an if-statement to see if a full image was uploaded or not, and then choosing which variable to use.
2. Simply change the form input names. i.e. For the full image <input type="file" name="full_image">
For the thumb, <input type="file" name="thumb_file">

My next suggestion is to remove the quotes from the $result objects (but it's just a suggestion).
i.e. unlink($result->image_large);

Edited by rc69, 09 January 2006 - 09:17 PM.


#6 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 09 January 2006 - 10:08 PM

Ok thanks, I thought it would, So it would be come this right?

$thumb = "images/items/".$_FILES["thumb"]["name"];
copy($_FILES["thumb"]["tmp_name"], $thumb);

That made it easier.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users