[英]when i click on delete button next image is deleted not the image i clicked in php mysql image gallery
表格上传图片
<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='file'>
<input type='submit' value='Upload'>
</form>
php编码,用于从创建用于存储要删除图像的ID的数据库会话插入和显示图像库
<?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会话ID将被删除
<?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");
?>
我看不到从哪里获取id
值来设置del_id.
但是,您将需要以下内容:
更换:
echo "<form action=\"\" method=\"post\"><input type=\"submit\" name=\"del\" value=\"submit\"></form><a href=\"$image\"><img src=\"$image\" /></a>";
有了这个:
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>";
并更改$_SESSION['del_id'] = $id;
到$_SESSION['del_id'] = $_POST['del_id'];
$_SESSION['del_id'] = $id;
您从哪里获取此$ id?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.