How to delete a row from the database?

Asked

Viewed 220 times

-2

The name of my table in the database is arquivo. Then there is a list of images and I want to solve, all in PHP

Here I exhibited

<?php
include ("cabecalho-tcc.php");
include("rodape-tcc.php");
//CONEXÃO COM O BANCO DE DADOS
//passando os dados necesários do banco de dados
$banco = new mysqli("localhost","root","","atlas");
//recuperar os arquivos (tabela)
$sql = "SELECT * FROM arquivo";
// irá receber o resultado da função query
$resultado = $banco -> query ($sql);

//manibular o resultado para salvar uma nova variavel, para exibir o banco de dados
while ($linha = mysqli_fetch_array($resultado)) {
    # variavel vetor, ou seja, a cada laço de repetição do while será acrescentada 
    #uma nova linha, que refere aos atributos armazenados 
    $album[] = $linha;
}

?>
<body>
    <!--EXIBIR AS IMAGENS DENTRO DE UMA TABELA-->
    <table class="table table-striped table-sm">
        <tr>
            <td scope="col">Imagem</td>
            <td scope="col">Descrição</td>
            <td scope="col">Remover Imagem</td>
        </tr>
        <tr>
            <?php
                //pegar todas a linhas que existe no album e passa para a imagem
                foreach ($album as $imagem):
            ?>
            <td width="200" height="200">
                <img class="img-thumbnail" src="<?php echo "./upload/". $imagem['imagem']?>" >
            </td>
            <td>
                <?= $imagem['descricao']?>
            </td>
            <td>
                <a href="remove-imagem.php" class="text-danger">Remover</a>
            </td>
        </tr>
        <?php  endforeach //fechando ?>
    </table>
</body>

Here deletes

<?php
include ("cabecalho-tcc.php");
include ("conexao_upload.php");
include ("rodape-tcc.php");

$id = 1;

$sql = mysql_query("SELECT imagem FROM arquivo WHERE id = '".$id."'");
$arquivo = mysql_fetch_object($sql);

$sql = mysql_query("DELETE FROM imagem WHERE id = '".$id."'");

unlink("upload/".$arquivo->imagem."");
?>
  • And what’s the problem? Is there a mistake? Which one? Where?

  • Notice: Undefined index: id in C: xampp htdocs tcc remove-imagem.php on line 7 Warning: mysql_fetch_object(): supplied argument is not a Valid Mysql result Resource in C: xampp htdocs tcc remove-imagem.php on line 11 Notice: Trying to get Property of non-Object in C: xampp htdocs tcc remove-image.php on line 16 Warning: unlink(upload/): Permission denied in C: xampp htdocs tcc remove-image.php on line 16

  • I want I delete the image that the user click on remove

1 answer

0


Looking at your code to delete images, the first error I encounter is that you will always try to delete the "image" with id = 1 in the database, because Voce declared: $id = 1;, then you should develop a solution to pass the image id to your algorithm.

Now let’s go to the errors that the application itself warns:

  1. Notice: Undefined index: id in C:\xampp\htdocs\tcc\remove-imagem.php on line 7 this indicates that there is no supposed index ['id'] in an array, but if code does not have an array. There must have been a mix-up here.
  2. Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in... This error happens by the syntax error in your SQL command previously shown.

Then I came to the conclusion that you must first fix the query that "recovers" the image in the database, and then perform any operation with it.

  • Then my variable id would have to be an array, but like $_POST? because my database would choose id.

  • not necessarily $_POST, just needs the image id value to dynamically arrive at the algorithm

Browser other questions tagged

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