Click to See Complete Forum and Search --> : The Update not working


Alan P
06-21-2007, 04:27 PM
I've been wrestling with this script all day with no luck at all. I have to test both parts of this script, successfully in order to finish this project. I haven't tested the delete part yet because I'm still trying to get the update part to work. I don't know what it is, but it just will not update the database.

<?php
include("includes/dbconnect.php");

if(!isset($cmd))
{
$result = mysql_query("select * from $table order by title");
while($r = mysql_fetch_array($result))
{

$id = $r["id"];
$title = stripslashes($r["title"]);

echo "

<tr>
<td width='20%' align='right'>ID#: </td>
<td>$id</td>
</tr>
<tr>
<td width='20%' align='right'>Picture Title: </td>
<td>$title</td>
</tr>
<tr>
<td width='20%' align='right'>Picture File Name: </td>
<td>$image_name</td>
</tr>
<tr>
<td width='20%' align='right'>[ <a href='edit_picture.php?cmd=edit&id=$id'>Edit</a> ]</td>
<td>[ <a href='edit_picture.php?cmd=delete&id=$id'>Delete</a> ]</td>
</tr>
<tr><td colspan='4' width='100%' style='border-top: 1px solid #666'>&nbsp;</td></tr>

";

}
}
?>

</table>

<?php

echo "<h3>Edit a Picture</h3>";
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM $table WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

<center>
<table width="95%" cellspacing="0" cellpadding="3" border="0">
<tr><td>
<form action="edit_picture.php" method="post">

<input type="hidden" name="id" value="<?php echo $myrow['id'] ?>">


Picture Title:<br>
<input type="text" name="title" VALUE="<?php echo stripslashes($myrow['title']) ?>" size="55"><br><br>

Picture Description:<br>
<TEXTAREA name="desc" ROWS=2 COLS=70 wrap=virtual><?php echo stripslashes($myrow['desc']) ?></TEXTAREA><br><br>

<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
</td>
</tr>
</table>
</center>

<?php
}
?>

<?php

if ($_POST["$submit"])
{

$title = addslashes($_POST['title']);
$desc = addslashes($_POST['desc']);

$sql = "UPDATE $table SET title='$title', desc='$desc' WHERE id=$id";

$result = mysql_query($sql);
echo "<br><br><p align='center'><span style='color:red'>Information updated.</span><br><br><a href='edit_picture.php'>Edit another Picture</a></p>";

}
}

?>

<?php
if($_GET["cmd"]=="delete")
{

$image_name = $_GET["image_name"];

$photo_dir = ('/mysite.com/gallery/image_uploads/');
$thumb_dir = ('/mysite.com/gallery/thumb_uploads/');

unlink($photo_dir.$image_name);
unlink($thumb_dir.$image_name);

$sql = "DELETE FROM $table WHERE image_name=$image_name";
$result = mysql_query($sql);

echo "<br><br><p align='center'><span style='color:red'><b>The Picture has been Deleted!<br><br>";
}

?>

Kyleva2204
06-21-2007, 06:00 PM
I don't really know if this causes an issue or not, however I haven't ever set a mysql_query to a variable.. I don't know if that would cause it however.
-- sorry just did a test.. doesn't affect it. For more detail however, add or die(mysql_error()) to the end of your mysql_query.. that way if its having a problem it will tell you what it is.

mysql_query($query) or die (mysql_error());

Alan P
06-21-2007, 09:18 PM
Yes, I should have added the mysql error before but overlooked it. I added it and discovered what the problem was... my row name: name="desc". 'desc' is a MySQL keyword so I changed it to image_desc and now the update works fine.

Now if I can get the delete portion working I'll be finished with this project. I haven't found anything in my books or on the web about deleting a image from the server. I don't know, maybe PHP can't handle this kind of a job. I tried to use what I put up above but it doesn't work. I get an error message saying this is a directory or file doesn't exist. I can't make it work.

jasonahoule
06-21-2007, 09:25 PM
You can use the unlink() function to delete files, just remember that you need the absolute path. Try using something like this...

unlink($_SERVER['DOCUMENT_ROOT'].'/PATH_TO_IMAGE/'.$image);

Sheldon
06-21-2007, 09:26 PM
http://php.net/unlink

Alan P
06-21-2007, 09:38 PM
Jason, I tried it but it returned this:
Warning: unlink(/local/home/user/mysite.com/gallery/image_uploads/): Is a directory

Sheldon, thanks for the link. I was on that page a couple of days ago. It know I need to use unlink, but I don't know how to use it. This is much different than deleting from a database, which is quite simple and straight forward. But this, I know nothing about and can't find much about it in my books or on the Web.

My book - PHP and MYSQL Web Development has just a short 5 line paragraph, out of nearly 950 pages, pertaining to unlink. My other 2 books I don't think even mentions it.

Unlink, is most definitely the red-headed step child of PHP.

jasonahoule
06-21-2007, 09:42 PM
Did the delete from the database work? Your image name is missing. Are your sure that the $image_name variable is being set?

jasonahoule
06-21-2007, 09:43 PM
If you are trying to delete the directory then you need to use rmdir().

Alan P
06-21-2007, 09:48 PM
No no, I don't want to delete the directory, just the image.

No, image info is still in database. I don't think that it will delete from there until it can get past the unlink part.

jasonahoule
06-21-2007, 09:57 PM
The two should be completely unrelated. You are only storing the image name in the database, right?
I see what the problem is. You are not passing the name in the query string. You are only passing the id. You will either need to pass the name also or lookup the name using the id. Right now your $image_name variable is an empty string. Also, you should be doing the delete on the id not the image name.

Alan P
06-21-2007, 10:03 PM
I changed it to this a short time ago, so the current error messages or for this set up.<a href='edit_picture.php?cmd=delete&image_name=$image_name'>Delete</a>
This is the delete portion as it stands right now.
<?php
$image_name = $_GET["image_name"];

if($_GET["cmd"]=="delete")
{

unlink($_SERVER['DOCUMENT_ROOT'].'/gallery/image_uploads/'.$image);
unlink($_SERVER['DOCUMENT_ROOT'].'/gallery/thumb_uploads/'.$image);

$sql = "DELETE FROM $table WHERE image_name=$image_name";
$result = mysql_query($sql);

echo "<br><br><p align='center'><span style='color:red'><b>The Picture has been Deleted!<br><br>";
}
?>

jasonahoule
06-21-2007, 10:20 PM
These should be

unlink($_SERVER['DOCUMENT_ROOT'].'/gallery/image_uploads/'.$image_name);
unlink($_SERVER['DOCUMENT_ROOT'].'/gallery/thumb_uploads/'.$image_name);