Deleting an image in PHP

Problem in deleting an image in DB and Source Folder

Page 1 of 1

3 Replies - 1256 Views - Last Post: 30 August 2009 - 01:13 PM Rate Topic: -----

#1 Jupiter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 01-February 07

Deleting an image in PHP

Posted 28 August 2009 - 11:17 AM

Hi to all reading this topic B)

Im having some problem in deleting an image from the source folder and from the database[DB], well i guess u can help me with this :)

Database Schema:

manage_pic {pic_id, filename, info}


<?php 
	 
	include("validate_session.php");
	$loginname=$_SESSION["loginname"];
	
	include("database_connection.php");
	
	$pic_id=$_GET['picid']; //getting this from a previous page 
	
	$query = mysql_query("SELECT * FROM manage_pic WHERE pic_id='$pic_id'");
	
	if(!$query) // if cannot retrieve, display error message
	{
		die('ERROR: Could not RETRIEVE DATA from table. ' . mysql_error());
	}
	
	$result = mysql_fetch_array( $query );
	
	if(!$result) // if cannot retrieve, display error message
	{
		die('ERROR: Could not RETRIEVE DATA from table. ' . mysql_error());
	}
	
	$info= $result['info'];
	$prod_serv_name= $result['prod_serv_name'];
	
	mysql_close($con);
?>
<html>
	<head>
	<title>.</title>
		<script language="Javascript">
	<!--
	  function confirmDelete()
	  {
		input_box=confirm("Are you sure you want to delete all content of this category?");
		 if (input_box==true)
		{
			// Output when OK is clicked
			 		   window.location.replace("delete_pic_pro.php?picid=<?php echo "$pic_id"; ?>")
		}
		else
			return false;
	}
	-->
</script>
</head>
<body>
<div align="center" id="error" >
 <?php
	 if (isset($_SESSION['error']))
	 {
	echo $_SESSION['error'];
		unset($_SESSION['error']);
	 }
 ?>
</div>
<?php
	echo "<img src=\"../content_images/" . $result['filename'] . "\" alt=\"\"  />";
?>
</body>
</html>



Next page[i.e the processing page]
delete_pic_pro.php

<?php 
	 
	include("validate_session.php");
	$loginname=$_SESSION["loginname"];
	
	include("database_connection.php");
	
	$pic_id=$_GET['picid'];
	//echo "$pic_id";
	
	$sql_delete="DELETE FROM manage_pic WHERE pic_id=$pic_id";
	unlink($filename);
	header("Location: admin_show_content.php");
	
	if(!$sql_delete) // if cannot retrieve, display error message
	{
		die('ERROR: Could not DELETE DATA from table. ' . mysql_error());
		$_SESSION['error'] = "A problem occurred, cannot delete.";
		header("Location: admin_edit_content.php?picid='$pic_id'");
	}

	mysql_close($con);
?>


Problem:
The problem im getting is that the picture is not being deleting from the image/source folder and not even in the database :(
Can u please tell me if im using the unlinck function right or even the delete query if its ok.. i cant find the problem myself :crazy:

Thx ;)

Note: Some parts of the code like basic HTML have been omitted for simplicity purposes :)

This post has been edited by Jupiter: 28 August 2009 - 11:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Deleting an image in PHP

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Deleting an image in PHP

Posted 28 August 2009 - 11:37 AM

the $filename variable comes from out of nowhere?

You never execute the delete-query?

:)

$filename = "get filepath from database, right?";

$sql_delete="DELETE FROM manage_pic WHERE pic_id=$pic_id";
$result = mysql_query($sql_delete);
if($result)
{
//chmod($filename, 0777); // you may have to include this line to open up the privileges
unlink($filename);
}
else
{
die(mysql_error());
}

or something! :)
Was This Post Helpful? 1
  • +
  • -

#3 Jupiter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 01-February 07

Re: Deleting an image in PHP

Posted 30 August 2009 - 10:13 AM

:stupid: my mistake i've forgot to execute the query :blush:

here are the codes below, its working now :D

<?php 
	 
	include("validate_session.php");
	$loginname=$_SESSION["loginname"];
	
	include("database_connection.php");
	
	$pic_id=$_GET['picid'];
	
	$query = mysql_query("SELECT * FROM manage_pic WHERE pic_id='$pic_id'");
	
	if(!$query) // if cannot retrieve, display error message
	{
		die('ERROR: Could not RETRIEVE DATA from table. ' . mysql_error());
	}
	
	$result = mysql_fetch_array( $query );
	
	if(!$result) // if cannot retrieve, display error message
	{
		die('ERROR: Could not RETRIEVE DATA from table. ' . mysql_error());
	}
	
	$file= $result['filename'];
	
	$filename = "../content_images/$file";

	$sql_delete=mysql_query("DELETE FROM manage_pic WHERE pic_id=$pic_id");
	unlink($filename);
	header("Location: admin_show_content.php");
	
	if(!$sql_delete) // if cannot retrieve, display error message
	{
		die('ERROR: Could not DELETE DATA from table. ' . mysql_error());
		$_SESSION['error'] = "A problem occurred, cannot delete.";
		header("Location: admin_edit_content.php?picid='$pic_id'");
	}

	mysql_close($con);
?>



thanks Wimpy :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Deleting an image in PHP

Posted 30 August 2009 - 01:13 PM

No problem, happy to help! :)

View PostJupiter, on 30 Aug, 2009 - 07:13 PM, said:

thanks Wimpy :bigsmile:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1