Unlink image and delete from mysql

I am trying to delete an image from a directory and also delete the image name from the database but it isn’t working :bawling:

This may be something simple I have in the wrong place but I am really not sure. I you could please take a look at the code I am using.


if ($action == "deleteimage")

{
$result = mysql_query("SELECT * FROM categories",$link);
while ($a_row = mysql_fetch_array ($result) )
{
 unlink("/home/skgaston/public_html/CMS/full/pics/$a_row[image]");
}
$query = "UPDATE categories set image = '$a_row[image]' WHERE (id = '$id')";
if (!mysql_query ($query, $link) )
	{
	die (mysql_error());
	}
print "<table width=600 align=center bgcolor=ffffff><tr><td>Your image has been removed...</td></tr></table>";
}

Also I am passing the variable as
<a href=\“editcat.php?action=deleteimage\”>

one thing you might look into changing is using mysql_fetch_assoc instead of mysql_fetch_array - i think it will make more of an array like what you’re looking for.

you might also echo out the value of a_row[image] each time in the while just to be sure it’s set…

I tried that and it is unlinking the image. But I think I have something wrong with the UPDATE query, Not realy sure though.

well, if you’re trying to remove that record from the database, then an UPDATE isn’t the right one to use.

You’d probably want a DELETE, like this:


DELETE from categories where image='$a_row[image]'

Otherwise, if you’re just trying to update it, I don’t think $id is set there, unless you set it someplace I can’t see.

Well I still can’t seem to get it :bawling:

Anyway I am going to post the guts of the page so maybe you can see what I am exactly doing. Also I am getting this error after I try and delete the image:
Error fetching category details: You have an error in your SQL syntax near ‘’ at line 1
Which I beleive has something to do with the $id variable not being populated after the deletion.


<?
//################# Delete Image from Folder
			if ($action == "deleteimage")

				{
  $result = mysql_query("SELECT * FROM categories",$link);

while ($a_row = mysql_fetch_assoc ($result) )
{
 unlink("/home/skgaston/public_html/CMS/full/pics/$a_row[image]");
}
$query = "DELETE FROM categories where image = '$a_row[image]'";

		if (!mysql_query ($query, $link) )
			{
			die (mysql_error());
			}
print "<table width=600 align=center bgcolor=ffffff><tr><td>Your image has been removed...</td></tr></table>";

}
//################# UPLOAD IMAGE TO FOLDER #################
			if ($action == "upload")
{
//All the upload stuff
}
//############# If submit
if ($submit): // The category's details have
                // been updated.

  $sql = "UPDATE categories SET
          Name='$name'
					image='$userfile'
          WHERE ID=$id";
  if (@mysql_query($sql)) {
    echo("<p>Category details updated.</p>");
  } else {
    echo("<p>Error updating category details: " .
         mysql_error() . "</p>");
  }

?>

<p><a href="cats.php">Return to Category list</a></p>

<?php
else: // Allow the user to edit the category
        // with ID=$id

{

  $cat=@mysql_query("SELECT * FROM categories WHERE ID=$id");
  if (!$cat) {
    echo("<p>Error fetching category details: " .
      mysql_error() . "</p>");
  //  exit();
  }

  $cat = mysql_fetch_array($cat);

  $name = $cat["name"];
  $image = $cat["image"];
  // Convert special characters for safe use
  // as an HTML attribute.
  $name = htmlspecialchars($name);

?>

<form action="<?=$PHP_SELF?>" method="post">
<p>Edit the category:<br />
Name: <input type="text" name="name" value="<?=$name?>" size="20" maxlength="255" /><br />

<input type="hidden" name="id" value="<?=$id?>" />
<input type="submit" name="submit" value="SUBMIT" /></p>
</form>
<?

if ($image == "")
//###################################

{
print "<CENTER><B>Manage Images</b></center><P>";
print"<FORM METHOD=\\"post\\" ACTION=\\"editcat.php\\" ENCTYPE=\\"multipart/form-data\\">";
print"<INPUT TYPE=\\"hidden\\" NAME=\\"edit\\" VALUE=\\"$propnum\\">";
print"<INPUT TYPE=\\"hidden\\" NAME=\\"action\\" VALUE=\\"upload\\">";
print"<INPUT TYPE=\\"hidden\\" NAME=\\"id\\" VALUE=\\"$id\\">";
print"<TABLE BORDER=\\"0\\" cellpadding=3>";
print"<TD>File: </TD>";
print"<TD><INPUT TYPE=\\"file\\" NAME=\\"img1\\"></TD>";
print"</TR>";
print"<TR>";
print"<TD COLSPAN=\\"2\\"><INPUT TYPE=\\"submit\\" VALUE=\\"Upload\\"></TD>";
print"</TR>";
print"</TABLE>";
print"</FORM>";
print "</center></td></tr></table>";
}

else
{
print "<CENTER><img src=\\"./pics/$image\\"</center><form action=\\"editcat.php\\" method=\\"post\\"><input type=\\"hidden\\" name=\\"id\\" value=\\"$id\\"><input type=\\"submit\\" value=\\"deleteimage\\" name=\\"Delete\\"></form><a href=\\"editcat.php?action=deleteimage\\">Delete This Image</a>";
}
}
?>

hhmmmm

you could try removing the $link var from the mysql_query, also if u click the “delete this image” text link it doesnt pass an ID, which cause fetching cat details error, put &id=$id in The print statement at the end of the text link.

other than that i dont know why it doesnt work

Passing the ID with the link fixed the error but it still isn’t deleting the image name from my table. Oh well I am not sure what to do?

silly question, but does the cable “categories” have a column named “image”, or is it the “ID” that you’re looking for?

The catagories table is set up like this:
ID name image

So whenever I want to delete an image I would like the image address to be removed from the image column.

Sorta like

1 tips 12345.gif
2 tricks 123678.gif

Then when I delete the image I would like to keep the ID and name.

1 tips
2 tricks 123678.gif

Hope this makes it simplier.


$sql="update categories set image='' where ID='$id'";

remember, MySQL is case-sensitive…

Yes I have taken that out, but still doesn’t work.:mad: