I have a query page that shows the entries made in the database through a form and a button with the option to delete.
Query page:
<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 treco:
If you can point me to the error or indicate a better way to do this, then you would be happy. :)