Delete record button does not work

Asked

Viewed 742 times

0

I have a query page that shows the entries made in the bank through a form and a button with the option to delete.

Page of the consultation:

<h1 style="
    text-align: center;
    height: 7;
    margin-top: 150;
    margin-bottom:70;
"> Consulta de formações </h1>

<!--Filtro de busca-->
<form>
    <div class="col-lg-3">
        <div class="form-group">
            <label for="NOME">Nome: </label>
            <input class="form-control" id="NOME" placeholder="Nome do colaborador" name="NOME">
        </div>
    </div>
    <button href="acoes.php?acao=delete" class="btn btn-primary" style="margin-top: 22;">Buscar</button>
</form>
<?php
//Conexão e consulta ao Mysql
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_formacao') or die(mysql_error());
$qry = mysql_query("select * from formacoes");

//Pegando os nomes dos campos
$num_fields = mysql_num_fields($qry);//Obtém o número de campos do resultado

for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
    $fields[] = mysql_field_name($qry,$i);
}

//Montando o cabeçalho da tabela
$table = '<table class="table table-hover table-inverse" style="margin-top:50;background-color: #37444a; color:lightgrey;"> <tr>';

for($i = 0;$i < $num_fields; $i++){
    $table .= '<th>'.$fields[$i].'</th>';
}

//Montando o corpo da tabela
$table .= '<tbody style="
    background-color: #86979e;
    color: #37444a;    
">';
while($r = mysql_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }

    // Adicionando botão de exclusão
$table .= '<td><button href="deleteF.php" class="btn btn-danger">Excluir</button></td>
           <form method="post" action="deleteF.php">
                <INPUT TYPE="hidden" NAME="ID" VALUE="ID">
            </form>';
$table .= '</tr>';

}

//Finalizando a tabela
$table .= '</tbody></table>';

//Imprimindo a tabela
echo $table;

?>

deleteF.php:

<?php

    $id = $_POST['ID'];

    $strcon = mysqli_connect('localhost','root','', 'db_formacao') or die('Erro ao conectar ao banco de dados');
    $sql = "DELETE FROM `db_formacao`.`formacoes` WHERE ``formacoes`.`ID` = $id";
    mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    mysqli_close($strcon);

    echo '<script type="text/javascript">
            alert("Registro deletado!");
            window.history.go(-1);
        </script>';
?>

The thing:

If you could point out the mistake or indicate a better way to do it, I’d be happy. :)

  • 1

    Should have an Hidden input with the id value and a form with action for delete, no sql has a typo.

  • But where would I put this action? Because this form that you find in the question is the form of the search field there, not the form used to register the information.

  • For each line found you need a new form with the action deleteF.php which is the file that makes the delete and will also need an Hidden field called ID to properly run the line $id = $_POST['ID'];

  • I’ll edit it showing how I did it, if you can take a look...

1 answer

2


The first error is in this section of the query page:

//Montando o corpo da tabela
$table .= '<tbody style="
    background-color: #86979e;
    color: #37444a;    
">';
while($r = mysql_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }

    //eu tirei o código de dentro do for, agora ele vai gerar só um botão cada linha
    /*aqui você tem um button que serve como um submit, mas ele tem que estar dentro de um form para que envie o post. Então aqui eu mudei umas coisinhas*/
        $table .= '<td><form action="deleteF.php" method="post">'; //formulário com método post que vai para deleteF.php
        $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">'; //Nesse value tem que estar o ID do curso
        $table .= '<button type="button" class="btn btn-danger">Excluir</button>'; //aqui está o seu botão
        $table .= '</form></td>'; //só fechando o form

    $table .= '</tr>';
}

Now your deleteF.php file had two typos in the query; formacoes has 2 quotes and the $id variable has to be concatenated. would look like this:

$sql = "DELETE FROM `db_formacao`.`formacoes` WHERE `formacoes`.`ID` = " . $id;
  • 1- You explain very well explained. 2-I took the $table of the while because it was generating a button for each item of the record, other than that, I made the recommended modifications and is not yet excluding. :/

  • but if you don’t want 1 button for each record, it will be a button that deletes all?

  • That line there is a record, it’s "course", so when you delete, you have to delete the entire course, all the items.

  • then in input ID has to be the course id. I edited the answer, take a look ;)

  • Ah, got it! So, I made the changes indicated, but it’s not working yet. I looked at everything and it looks all right, I don’t understand why. :(

  • OPA, DELETED YES! The error was mine, I forgot to put the file directory. -' kkkkkkkk Thanks!

  • wonder! good that solved :D

Show 2 more comments

Browser other questions tagged

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