Rooban M Rooban M - 5 months ago 15
MySQL Question

when i click on delete button next image is deleted not the image i clicked in php mysql image gallery

form to upload images

<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='file'>
<input type='submit' value='Upload'>
</form>


php coding to insert and display image gallery from database session created to store id of the image to delete

<?php
session_start();
$conn = mysqli_connect("localhost","root","","img") or die ("connection failed ");
if ($_FILES)
{
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$insert = "insert into image (image) values ('".$name."')";
$sql = mysqli_query($conn,$insert);
}
$view = mysqli_query($conn,"select * from image");
while($row = mysqli_fetch_array($view))
{
extract($row);
echo "<form action=\"\" method=\"post\"><input type=\"submit\" name=\"del\" value=\"submit\"></form><a href=\"$image\"><img src=\"$image\" /></a>";
if(isset($_POST['del']))
{
$_SESSION['del_id'] = $id;
header("location:del.php");
}
}
?>


del.php session id will be deleted

<?php
session_start();
$conn = mysqli_connect("localhost","root","","img") or die ("connection failed ");
$id = $_SESSION['del_id'];
$sql = mysqli_query($conn,"delete from image where id = '$id' ");
header("location:x.php");
?>[1]


http://programscript.com

Answer

I don't see where you are getting the id value from to set del_id. However, you would need something like this:

replace:

 echo "<form action=\"\" method=\"post\"><input type=\"submit\" name=\"del\" value=\"submit\"></form><a href=\"$image\"><img src=\"$image\" /></a>";

With this:

echo "<form action=\"\" method=\"post\">";
echo "<input type=\"submit\" name=\"del\" value=\"submit\">";
echo "<input type=\"hidden\" name=\"del_id\" value=\"$row['id']\">";
echo "</form>";
echo "<a href=\"$image\"><img src=\"$image\" /></a>";

And change $_SESSION['del_id'] = $id; to $_SESSION['del_id'] = $_POST['del_id'];