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

时间:2016-03-02 07:22:33

标签: php

我有这个代码,它应该同时从两个不同的文件夹中删除两个图像文件。问题是,只删除一个图像。另一个不是。我尝试过不同的方法,但问题仍然存在。

以下是我的代码。

<?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");
    }
}    

表格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 ;

1 个答案:

答案 0 :(得分:1)

想想,只需替换此代码

$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']);
}

使用此代码。

$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]);
}

<强>建议

重命名变量$ imID,因为它实际上是albumId。正如您在代码中可以看到的那样

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