从路径和数据库中删除图像


delete image from path and database

我收到以下错误:

警告:unlink() [function.unlink]:中的参数无效 C:''xampp''htdocs''SH''owner''delete_img.php 在第 11 行

您的 SQL 语法有误;请查看相应的手册 到您的 MySQL 服务器版本,以便在附近使用正确的语法 '* 从第 1 行的 img_homestay 中 IMGID="73"''

这是我删除图像的代码。我的图像有自己的图像。我想使用他们的 imgid 删除它。

<?php
// This is a sample code in case you wish to check the username from a mysql db table
$link=mysql_connect("localhost", "root","")or die("could not connect");
$db=mysql_select_db("sh",$link) or die ("could not select database");
    $imgid = $_GET['imgid'];
    // sending query
    $select=mysql_query("SELECT location FROM img_homestay WHERE imgid='$imgid'");
    $img=mysql_fetch_array($select);
    unlink($img['location']);
    $result=mysql_query("DELETE * FROM img_homestay WHERE imgid='$imgid'")
    or die(mysql_error());      
    header("Location: editimage1.php");
?>

这是我要删除的其中一张图片的链接:

/SH/owner../data/img1.jpg

来自评论。它已被编辑。

您的查询应DELETE FROM 。从查询中删除*

完整查询:DELETE FROM img_homestay WHERE imgid='$imgid'

此外,您可以从查询中删除$result =部分,也可以执行此操作。

if($result) {
    //Successfully deleted image.
}

此外,您的代码极易受到 SQL 注入的影响。您正在将原始用户输入传递到 SQL 查询中。至少,您应该转义引号,但强烈建议您也使用 MYSQLI 或 PDO 函数集进行数据库连接和查询。

您可能还需要检查您尝试从中删除文件的文件夹的权限。理想情况下,应将包含图像的文件夹设置为 chmod 755

http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/

此链接是对准备好的陈述的非常基本的介绍,还提供了进一步阅读的链接。

编辑:完整片段。

<?php
// This is a sample code in case you wish to check the username from a mysql db table
$link=mysql_connect("localhost", "root","")or die("could not connect");
$db=mysql_select_db("sh",$link) or die ("could not select database");
$imgid = $_GET['imgid'];
$imgid = mysql_real_escape_string($imgid);
$path= $_SERVER['DOCUMENT_ROOT'].'/owner../data/';
// sending query
$select=mysql_query("SELECT location FROM img_homestay WHERE imgid='$imgid'");
$img=mysql_fetch_array($select);
unlink($path.$img['location']);
$result=mysql_query("DELETE FROM img_homestay WHERE imgid='$imgid'") or die(mysql_error()); 
//Check to see if the query can run
if($result) {
    header("Location: editimage1.php");
} else {
    //Query failed. Display an error message here.
}

?>

<?php
// This is a sample code in case you wish to check the username from a mysql db table
$link=mysql_connect("localhost", "root","")or die("could not connect");
$db=mysql_select_db("sh",$link) or die ("could not select database");
    $imgid = $_GET['imgid'];
    $path= $_SERVER['DOCUMENT_ROOT'].'/owner../data/';
    // sending query
    $select=mysql_query("SELECT location FROM img_homestay WHERE imgid='$imgid'");
    $img=mysql_fetch_array($select);
    unlink($path.$img['location']);
    $result=mysql_query("DELETE FROM img_homestay WHERE imgid='$imgid'") or die(mysql_error());     
    header("Location: editimage1.php");
?>

这种类型的错误基本上需要调试。首先检查问题是否与unlink()函数或SQL连接有关。创建另一个 php 文件 n 检查它是否适用于静态路径

<?php
$path='/SH/owner../data/img1.jpg'; // update it as per your filepath..path should be from root
if(unlink($path)) 
{
echo "Deleted file ";
}
else
{
 echo "Not Able to Delete File";
}
?>

让我们看看它重新运行了什么。

作为 9997 编写的关于数据库连接和查询执行的部分似乎完全没问题。