4
I have a script simple PHP+Mysql query:
<table >
<tr>
<td ><h2>GERENCIAR ANUNCIOS </h2></td>
</tr>
<tr>
<td >
<table >
<tr>
<th >ID</th>
<th >Nome</th>
<th >Email</th>
<th >Ação</th>
</tr>
<?php
// Aqui você se conecta ao banco
$mysqli = new mysqli('127.0.0.1', 'root', '', 'login');
// Executa uma consulta
$sql = "SELECT `user_id`, `user_name` , `user_email` FROM `users`";
$query = $mysqli->query($sql);
while ($dados = $query->fetch_assoc()) {
$id = $dados["user_id"];
$nome = $dados["user_name"];
$email = $dados["user_email"];
echo "<tr>
<td > $id </td>
<td > $nome </td>
<td > $email </td>
<td > <a href='excluir.php?user_id=$id'>excluir </a></td>
</tr>\n";
}
echo 'Registros encontrados: ' . $query->num_rows . '<br><br>';
?>
</table>
</td>
</tr>
</table>
And a script excluir.php
<?php
$mysqli = new mysqli('127.0.0.1', 'root', '', 'login');
$id = 'user_id';
$strSQL = "DELETE FROM users WHERE user_id = $id";
mysqli_query($strSQL);
// Fechar conexão com Banco de Dados
mysql_close();
header("Location: index.php");
?>
I can’t delete records from the database. What I’m doing wrong?
Probably the solution will be what @Luis replied, but you should pass the id by
POST
and process the data entering theSQL
– Skywalker
Please note that when passing the
$id
through the link may have problems with Sqlinjection if it does not deal with that in query ofDELETE
.– Jorge B.