PHP unlink is doing delete to everything

1

I have this code: And I wanted to delete a file based on the directory address and file it is given, the problem is that it is deleting everything despite the argument (address) that only belongs to a file

DB.php

public function deleteFromFolder($path) {
        unlink($path);
}

public function fetchAllRandomImageHighlightsAdmin() {
    $sql = ("SELECT 'image_path' FROM 'highlight_image');
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

public function fetchAllSeveralWorkAdmin() {
    $sql = ("SELECT 'image_path' FROM 'several_work_image');
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

edit.php (delete random image)

            if (isset($_POST['idRandom'])) {
            $id = $_POST['idRandom'];

            foreach ($dataBase->fetchAllRandomImageHighlightsAdmin() as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageWorkRandom($id);
            header('Location: editLisa.php');
        }

edit.php (delete several work image)

            if (isset($_POST['idSeveral'])) {

            $id = $_POST['idSeveral'];

            foreach ($dataBase->fetchAllSeveralWorkAdmin() as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageSeveralWork($id);
            header('Location: editLisa.php');
        }
    
asked by anonymous 08.08.2014 / 14:57

1 answer

0

The problem is that I was selecting all the elements of each table and deleting them one by one. The correct way is:

DB.php

public function selectRandomImageToDelete($id) {
    $sql = ("SELECT 'image_path' FROM 'highlight_image' WHERE 'id' = $id");
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

public function selectSeveralImageToDelete($id) {
    $sql = ("SELECT 'image_path' FROM 'several_work_image' WHERE 'id' = $id");
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

edit.php (delete random image)

if (isset($_POST['idRandom'])) {
            $id = $_POST['idRandom'];

            foreach ($dataBase->selectRandomImageToDelete($id) as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageWorkRandom($id);
            header('Location: editLisa.php');
        }

edit.php (delete several image)

if (isset($_POST['idSeveral'])) {

            $id = $_POST['idSeveral'];

            foreach ($dataBase->selectSeveralImageToDelete($id) as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageSeveralWork($id);
            header('Location: editLisa.php');
        }
    
08.08.2014 / 15:53