Delete image from BD and server destination folder

Asked

Viewed 5,123 times

-1

I want to simultaneously delete images from a specific column ("logo") of a table with more than 05 columns, and from the server’s target folder ("upload").

But I was only able to delete not only the column ("logo") but all the other 05 columns of the BD table, that is, excluding the entire row, without deleting the image file referring to the folder table ("upload") of destination.

I’m using the script below:

        <?php 
        include 'conexao.php';

        $pasta = 'upload/';

        if (isset($_POST['deletar'])){
        $check = @$_POST['apagar'];
        foreach($check as $logo){
        $delcheck = mysql_query("DELETE FROM topo WHERE logo = '$logo'") or die (mysql_error());
        unlink($pasta.$delcheck['logo']);
        if ($delcheck >= '1'){
        echo '<script type="text/javascript">
        alert("Deletado com sucesso!");
        window.location.href = "listar.php";
        </script>';
        }else{
        echo '<script type="text/javascript">
        alert("Erro, tente novamente!");
        window.location.href = "listar.php";
        </script>';
        }
        }
        }
        ?>

        <form action="" method="POST" enctype="multipart/form-data"><br />
        <?php
        include 'conexao.php';
        $seleciona = "SELECT * FROM topo";
        $queryum = mysql_query($seleciona);
        while ($list = mysql_fetch_array($queryum)){
        $logo = $list['logo'];
        ?>
        <input type="checkbox" name="apagar[]" value="<?php echo $logo; ?>" readonly><?php echo $logo; ?><br />
        <?php
        }
        ?>
        <input type="submit" name="deletar" value="Excluir"><br />
        </form>

It has as I delete data from a single column of the table, without deleting it completely?

Could someone please let me know if this is possible, and if this is how I should do it?

From now on my thanks.

  • Since you do not want to delete the row but only values that are in the column vc does an update table set logo="" Where logo="name_a_ser_excluded". So all lines that have this value you want to exclude are empty.

1 answer

2


Why instead of using delete not use update?

$delcheck = mysql_query("UPDATE topo SET logo="" WHERE logo = '$logo'") or die (mysql_error()); 
unlink($pasta_imgs.'/'.$logo);

There it will leave empty in the column where it has the variable value $logo.

  • Hello Thalles Daniel, I would like to thank you for the tip, because it worked perfect, I only changed the unlink($pasta_imgs. '/'. $logo); for unlink($folder. '/'. $logo);. Once again QUARREL.

  • I could vote for someone who did the favour of negative I don’t know why the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.