Re: delete query



mac wrote:
hi all,
im creating a form wich wil upload images to a folder and
their names and other details to a database.
im able to do uploading but my delete function is not working, please
can anybody tell me mistake n help me??

Hello Mac.
It would be easier for us if you told us exactly what errors or what you are seeing to make you think it does not work.
Also your MySQL table structure might help too.
But with that said and from what I am seeing in your code here are a few ideas.


image_uploader.php
<?
if(isset($_REQUEST['sub']))
{
//print "<pre>";
//print_r ($_FILES);
$dir_path=$_SERVER['DOCUMENT_ROOT']."/uploadproject/images/".
$_FILES['img1']['name'];
//print $dir_path;
if(move_uploaded_file($_FILES['img1']['tmp_name'],$dir_path))
{
//print "sucessfull";
}
else
{
//print "error";
}
mysql_connect("localhost","root","");
mysql_select_db("mac");
$date1=date("Y-m-d H:i:s");
$sql="insert into images(imagename,image_type,created_date)
values('".$_FILES['img1']['name']."','".$_FILES['img1']['type']."','".
$date1."')";
mysql_query($sql);
print "<font color='blue' size='3'>Image Added sucessfully...</
font>";


}
?>

<form name="frm" action="" method="post" enctype="multipart/form-
data">
<table border="1" align="center">
<tr><td colspan="2" align="center">Image upload</td></tr>
<tr><td>Choose An Image</td><td><input type="file" name="img1" /> </
td>
</tr><tr><td></td><td><input type="submit" name="sub" value="Add
Image" />&nbsp;&nbsp;<a href="show_images.php" target="_blank">Show
images</a>
&nbsp;&nbsp;<a href="delete_images.php" target="_blank">Delete images</
a></td>

</table>
</form>

--------------------------
delete_images.php
---> some problem with this page
<?php
mysql_connect("localhost","root","");
mysql_select_db("mac");

@$id = $_POST["id"];
//@$delete = $_POST["delete"];
if ($id) {
mysql_connect("localhost","root","");
mysql_select_db("mac");

$sql = "DELETE FROM images WHERE id='$id'";
mysql_query($sql);
print "Deleted ". $img1 ." from database";
}
mysql_connect("localhost","root","");
mysql_select_db("mac");
$sql = "SELECT imagename FROM images";
$result = mysql_query($sql);
?>
<form method="post" action="show_images.php">
Picture: <select name="imagename">
<?php
while ($row = mysql_fetch_array(($result), MYSQL_ASSOC))
print "<option>" . $row["imagename"];
?>
</select>
<input type="submit" name="delete" value="delete">
</form>
-----------------------------

Ok from what I can tell you are linking to the delete_images.php page which gives you a list of ALL your images. You then want to select the image and click the delete button to have it deleted. If this is correct, you have a few problems with your processing.

The last form:
> <form method="post" action="show_images.php">
> Picture: <select name="imagename">
> <?php
> while ($row = mysql_fetch_array(($result), MYSQL_ASSOC))
> print "<option>" . $row["imagename"];
> ?>
> </select>
> <input type="submit" name="delete" value="delete">
> </form>
> -----------------------------

Needs to refer to the same delete_image.php page.

Change:
<form method="post" action="show_images.php">
to:
<form method="post" action="delete_images.php">

Secondly you will need to POST the $id value back to the page or there will be no reference to grab the image. You will need to rewrite your <option> syntax to include the $id.

change:
print "<option>" . $row["imagename"];
to:
print "<option VALUE=\"".$row["id"]."\">" . $row["imagename"];

This will create an array imagename[] (name or your select tag) and place it in the $_POST variable.

Then in your script extract 'id' by replacing:
@$id = $_POST["id"];
with
$id = $_POST['imagename'];

This should delete your image, you can verify by looking at the list or your DB.

Now with THAT said, this rewrite is nothing close to being the correct way to perform this action, there are better ways to go about it, but I am in a hurry and don't have time to point them all out. But that is the basics.

If you don't understand what the login of the rewrite, then post back and I will explain a bit later.

Good Luck
Scotty


.