删除行 - pdo时取消链接图像

时间:2015-05-14 23:32:34

标签: php mysql pdo unlink

我正在尝试unlink()来自文件夹的图像,而它对应于从数据库中删除行。从数据库中删除行很有效,但我无法使unlink起作用。

我用谷歌搜索了2个小时并试了很多东西,但它仍然没有用,我想我有一些小故障,但我无法弄明白。

你能建议尝试一下。

<?php
$pdo = connect();
if (isset($_GET['delete'])) {

    $delete_id =   $_GET['delete']; 

    $img = "SELECT slika FROM filmovi WHERE id = $delete_id";
    $q = $pdo->prepare($img);
    $q->execute();
    $row = $q->fetchAll();

    $sql = "DELETE FROM filmovi WHERE id = :filmID";
    $query = $pdo->prepare($sql);
    $query->bindParam(':filmID', $delete_id, PDO::PARAM_INT);   
    $query->execute();

    $image = $row['slika'];

    unlink("assets/img/movies/.'$image");

    header("Location: index.php?movies");
    die();

}

2 个答案:

答案 0 :(得分:2)

unlink("assets/img/movies/.'$image");

所以说$image包含&#34; image.jpg&#34;。你构建的路径是

assets/img/movies/.'image.jpg

你最想要的是

unlink("assets/img/movies/". $image);

答案 1 :(得分:0)

一条评论帮助了我,但他删除了它。我删除了FetchAll并只保留了Fetch。 它现在没有任何错误。 这是代码:

<?php
$pdo = connect();
if (isset($_GET['delete'])) {

    $delete_id =   $_GET['delete']; 

    $img = "SELECT * FROM filmovi WHERE id = $delete_id";
    $q = $pdo->prepare($img);
    $q->execute();
    $row = $q->fetch();

    $sql = "DELETE FROM filmovi WHERE id = :filmID";
    $query = $pdo->prepare($sql);
    $query->bindParam(':filmID', $delete_id, PDO::PARAM_INT);   
    $query->execute();

    $image = $row['slika'];

    unlink("assets/img/movies/$image");

    header("Location: index.php?movies");
    die();

}