简体   繁体   English

如何在PHP中一次从两个不同的文件夹中取消链接两个图像

[英]How to unlink two images from two different folders at once in php

I have this code which is supposed to delete two image files from two diffrent folders at the same time. 我有这段代码,应该从两个不同的文件夹中同时删除两个图像文件。 The issue is, only one image is deleted. 问题是,仅删除一张图像。 the other is not. 另一个不是。 I have tried different methods but still the same issue. 我尝试了不同的方法,但仍然是同一问题。

Below is my code. 下面是我的代码。

<?php
$colname_albumedit = "-1";
if (isset($_GET['arf'])) {
    $colname_albumedit = $_GET['arf'];
}

$query_album = "SELECT * FROM galbum WHERE alID = '" . $colname_albumedit . "'";
$result_album = mysqli_query($connKcla, $query_album);
$row_album = mysqli_fetch_assoc($result_album);
$totalRows_album = mysqli_num_rows($result_album);

$query_album_images = "SELECT * FROM gimage WHERE alID = '" . $colname_albumedit . "'";
$result_album_images = mysqli_query($connKcla, $query_album_images);
$row_album_images = mysqli_fetch_assoc($result_album_images);
$totalRows_album_images = mysqli_num_rows($result_album_images);


if ((isset($_POST["form_del"])) && ($_POST["form_del"] == "adalbumdel")) {

    $target = "../gallery/albums/";
    $targett = "../gallery/images/";

    $imID = $_GET['arf'];
    $sql_query = "SELECT alImage FROM galbum WHERE alID = $imID";
    $photoresult = mysqli_query($connKcla, $sql_query);
    $row_album = mysqli_fetch_assoc($photoresult);

    if (($row_album['alImage']) != 0) {
        unlink($target . $row_album['alImage']);
    }

    $sql_queryy = "SELECT albumRef FROM gimage WHERE albumRef = $imID";
    $photoresultt = mysqli_query($connKcla, $sql_queryy);
    $row_album_images = mysqli_fetch_assoc($photoresultt);

    if (($row_album_images['albumRef']) != 0) {
        unlink($targett . $row_album_images['albumRef']);
    }

    $query_del = "DELETE FROM galbum WHERE alID = $colname_albumedit";
    $result_del = mysqli_query($connKcla, $query_del);

    $queryy_del = "DELETE FROM gimage WHERE alID = $colname_albumedit";
    $resultt_del = mysqli_query($connKcla, $queryy_del);

    if ($result_del && $resultt_del) {
        $updateGoTo = "confirm.php";
        if (isset($_SERVER['QUERY_STRING'])) {
            $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
            $updateGoTo .= $_SERVER['QUERY_STRING'];
        }
        header("Location: " . $updateGoTo);
    } else {
        header("Location: error.php");
    }
}    

The table gImage looks like this: 表gImage看起来像这样:

CREATE TABLE IF NOT EXISTS gimage ( 
    imID bigint(20) NOT NULL AUTO_INCREMENT, 
    imImage varchar(255) DEFAULT NULL, 
    albumRef bigint(20) DEFAULT NULL, 
    PRIMARY KEY (imID), KEY alID (albumRef) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Think, just replace this code 想一想,只需替换此代码

$sql_queryy = "SELECT albumRef FROM gimage WHERE albumRef = $imID";
$photoresultt = mysqli_query($connKcla, $sql_queryy);
$row_album_images = mysqli_fetch_assoc($photoresultt);

if (($row_album_images['albumRef']) != 0) {
    unlink($targett . $row_album_images['albumRef']);
}

with this code. 这段代码。

$sql_queryy = "SELECT imImage FROM gimage WHERE albumRef = $imID";
$photoresultt = mysqli_query($connKcla, $sql_queryy);
$row_album_images = mysqli_fetch_all($photoresultt);
foreach ($row_album_images as $row) {
    unlink($targett . $row[0]);
}

Recommendation 建议

Rename your variable $imID cause it is in reality the albumId. 重命名变量$ imID,因为实际上它是albumId。 As you can see in your code in the lines 如您在代码行中所见

$imID = $_GET['arf'];
$sql_query = "SELECT alImage FROM galbum WHERE alID = $imID";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM