简体   繁体   中英

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");
?>

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'];

$_SESSION['del_id'] = $id;

您从哪里获取此$ id?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM