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. :)
Should have an Hidden input with the id value and a form with
action
for delete, no sql has a typo.– rray
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.
– Mariana Bayonetta
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 calledID
to properly run the line$id = $_POST['ID'];
– rray
I’ll edit it showing how I did it, if you can take a look...
– Mariana Bayonetta