我有一个管理面板,其中有一个允许管理员从数据库中删除行的页面。他们可以删除的每一行都有一个关联的图像存储在服务器上的目录中。图像路径也存储在数据库中。
我需要的是,当用户从表中删除一行时,我希望它也删除图像。
下面是我从表中删除行的PHP代码:
include('includes/temp.config.php');
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get the 'id' variable from the URL
$id = $_GET['id'];
// delete record from database
if ($stmt = $link->prepare("DELETE FROM templates WHERE id = ? LIMIT 1"))
{
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare SQL statement.";
}
$link->close();
// redirect user after delete is successful
header("Location: edit.php");
}
else
// if the 'id' variable isn't set, redirect the user
{
header("Location: edit.php");
}但我甚至不知道从哪里开始删除相关的图像...任何帮助都将不胜感激。
发布于 2015-08-07 20:46:52
<?php
require __DIR__ . "/config.php";
if( isset($_GET['id'])){
$id=$_GET['id'];
$res=mysql_query("SELECT image FROM register where id= '$id'");
$row=mysql_fetch_array($res);
$delete="delete from register where id= '$id'";
$de = mysql_query( $delete);
unlink("upload/".$row['image']);
if($de)
{
header("location:login.php");
}
else
{
echo "error";
}
}
?>https://stackoverflow.com/questions/12291337
复制相似问题