php - 使用 unlink() 从数据库中删除带有在 php 中上传的位置

标签 php mysql

尝试从本地磁盘取消链接我的文件时出现错误:

if(isset($_POST['delete'])){
    $name = $_POST['deleteEntryName'];
    $query = "select name,image from image where name =  '".$name."' ";
    $result = $db->query($query);

    if(mysqli_num_rows($result)>0)
        {
            $delQuery = "delete from image where name = '".$name."' ";
            $delResult = $db->query($delQuery);
            $delPath = $query['image'];
            unlink($delPath);
            alert("File successfully deleted");
            redirect("newEntry.php");
        }
        else
        {
            alert("File is not exist");
            redirect("newEntry.php");
        }
}
?>

其中图像是我的图像的路径:

    $path = "upload/". $picture_name;

    move_uploaded_file($picture_tmp,$path)

    $insQuery = "insert into image(name,image,price,description) values('".$name."','".$path."','".$price."','".$desc."')";

我收到“非法字符串偏移‘图像’错误,并且没有文件存在错误。 为什么??

最佳答案

您使用了错误的变量。正确的应该是从 mysqli 中获取的结果,但您还没有这样做。

当使用 fetch_assoc() 时(对于 mysqli,对于 pdo 它只是 fetch())你不必调用 num_rows 因为如果没有行 fetch_assoc 返回 null。

$name = $_POST['deleteEntryName'];
// WARNING: Possible SQL Injection here!
$query = "select name,image from image where name =  '".$name."' ";
// mysqli::query() returns a statement
$selStmt = $db->query($query);
// mysqli::fetch_assoc() returns an array with your columns
if ($selResult = $selStmt->fetch_assoc()) {
        $delQuery = "delete from image where name = '".$name."' ";
        $delResult = $db->query($delQuery);
        $delPath = $selResult['image']; // change here
        unlink($delPath);
        alert("File successfully deleted");
        redirect("newEntry.php");
    }

关于php - 使用 unlink() 从数据库中删除带有在 php 中上传的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786719/

相关文章:

php - facebook如何实现写入可扩展性?

php - 如何使用 PHP 将数据发送到另一个页面?

mysql - Google Cloud MySQL 和主主复制

mysql - 数据库关系(MySQL Workbench)

PHP file_exists else

php - 在 PHP 中用 PDO 替换/迁移旧的 mysql 语法

php - 网比 : phpUNit or SimpleTest?

php - 解析json字符串,其中包含html

具有基于其他操作结果的操作的 MySQL 查询

PHP/mySQL - 在 'CONCAT' 子句中使用 'AS' 和 'LIKE' 的结果