使用取消关联

时间:2016-09-01 22:47:18

标签: php unlink

您好我有这个简单的删除脚本,但我只能删除该文件夹中的一个文件。我尝试了一些不同的东西试图循环unlink但它不会工作。有谁知道它是如何完成的?

$id = $_GET['id'];
$sql_get = "SELECT shop.id, shop.overskrift, shop.pris, shop.text, shop_pictures.ShopPictures_id, shop_pictures.shop_pictures_file
            FROM shop INNER JOIN shop_pictures
            ON shop.id = shop_pictures.fk_shop_pictures_shop_id WHERE Id=$id";
$result_get = mysqli_query($connection, $sql_get);
$row = mysqli_fetch_all($result_get);

$sql = "DELETE FROM shop WHERE Id=$id";
$result = mysqli_query($connection, $sql);

$sql = "DELETE FROM shop_pictures WHERE fk_shop_pictures_shop_id=$id";
if(!empty($row)) {
    foreach($row as $data) {
        unlink('../../img/shop_pictures/'.$data['shop_pictures_file']);
    }
}
$result = mysqli_query($connection, $sql);

//header('Location: ../members.php');exit;

1 个答案:

答案 0 :(得分:2)

您的代码极易受SQL Injection影响,因此您可能希望解决此问题。但是,您需要遍历结果。

$row = mysqli_fetch_array($result_get);只获取一个结果,请确保使用类似的内容:

$row = mysqli_fetch_all($result_get, MYSQLI_ASSOC);

if(!empty($row) {
    foreach($row as $data) {
        unlink('../../img/shop_pictures/'.$data['shop_pictures_file']);
    }
}

或者你也可以这样做:

while ($row = mysql_fetch_array($result_get)) {
    unlink('../../img/shop_pictures/'.$row['shop_pictures_file']);
}

这应该指向正确的方向。