Delete Full user

Asked

Viewed 138 times

3

Good morning, I’m trying to delete the items from a list but without success. Listing.php

<html>
<head>
    <link rel="stylesheet" href="/assets/_css/menu.css"/>
    <meta charset="UTF-8"/>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="script.js"></script>
</head>
<body>
<?php
$conecta = mysqli_connect('localhost', 'root', '') or trigger_error(mysql_error());
// Tenta se conectar a um banco de dados MySQL
mysqli_select_db($conecta, 'car0000001') or trigger_error(mysqli_error());
mysqli_set_charset($conecta, 'utf8');

echo '<tbody>';

$sql = mysqli_query($conecta,'SELECT * FROM usuario');

while ($row = mysqli_fetch_assoc($sql)) {
    echo '<table>';
    echo '<thead>';
    echo '<th>Código</th>';
    echo '<th>Nome</th>';
    echo '<th>Endereço</th>';
    echo '<th>E-mail</th>';
    echo '<th>Telefone</th>';
    echo '<th>Celular</th>';
    echo '<th>Cadastro</th>';
    echo '<th>Editar</th>';
    echo '<th>Excluir</th>';


    echo '</thead>';
    echo '<tr>';
    echo '<td>' . $row['id_usr'] . '</td>';
    echo '<td>' . $row['nome_usuario_usr'] . '</td>';
    echo '<td>' . $row['endereco_usuario_usr'] . '</td>';
    echo '<td>' . $row['email_usuario_usr'] . '</td>';
    echo '<td>' . $row['fone_usuario_usr'] . '</td>';
    echo '<td>' . $row['cel_usuario_usr'] . '</td>';
    echo '<td><a href="cadastro-completo.php"><img src="assets/imgs/edit.png" width="20px"></a></td>';
    echo '<td><img src="assets/imgs/edit.png" width="20px"></td>';
    echo '<td><button type="submit" action="deleta-cadastro.php" value="">Deleta</button>';
    echo '<br>';
    echo '</tr>';
    echo '</table>';
}
?>
</body>
</html>

delete-register.php

<?php
conecta = mysqli_connect('localhost', 'root', '') or trigger_error(mysql_error());
// Tenta se conectar a um banco de dados MySQL
mysqli_select_db($conecta, 'car0000001') or trigger_error(mysqli_error());
mysqli_set_charset($conecta, 'utf8');



$id = $_GET['deleta'];
$deleta = mysqli_query($conecta,'DELETE FROM `usuario` WHERE `id_usr` =$usuario';
if($deleta == ''){
    echo "<script>alert('Houve um erro ao deletar!');
</script>";
}else{
    echo "<script>alert('Registro excluido com sucesso!');
</script>"; 
    ?>
  • "unsuccessful", What does that mean? Makes a mistake? What happens? That’s too vague

  • means I can’t make it work. There’s no mistake, and it doesn’t delete anything in the BD.

  • 1

    The construction of table tb is wrong. You are opening a tbody before the table.

  • Unfortunately, it’s not working.

2 answers

3

You’re waiting for the user id to come from $_GET['deleta'] but on your button, you are not passing the user id anywhere.

echo '<td><button type="submit" action="deleta-cadastro.php" value="">Deleta</button>';

Try to adjust this line to:

echo "<td><button type='submit' action='deleta-cadastro.php?deleta={$row['id_usr']}' value="">Deleta</button>";

To get the id to the script.

Another point:

$id = $_GET['deleta'];
$deleta = mysqli_query($conecta,'DELETE FROM `usuario` WHERE `id_usr` =$usuario';

You wait for the id to be removed through $id = $_GET['deleta']; but in the query you filter the column id_usr by the value of the variable $usuario.

Adjustment to

$id = $_GET['deleta'];
$deleta = mysqli_query($conecta,"DELETE FROM `usuario` WHERE `id_usr` =$id";

And one more remark:

Beware of single quotes and double quotes. PHP interprets the two variables differently. See:

$id = 15;

If you print this variable with:

echo 'O id do usuário é $id';
\\ O id do usuário é $id'

PHP will literally use $id instead of its value. When we use double quotes, the behavior changes. PHP processes the string for variables and replaces them with their respective value:

echo "O id do usuário é $id";
\\ O id do usuário é 15

See this answer for more information on the differences between quotation marks and double quotation marks

  • Thank you! You are beasts rsrsrs

1


The Submit button needs a form with a action. You are using a Submit button without form and the direct action on the button, which will not work. And missing send via GET the value of deleta which will be received at $_GET['deleta']. With this, the value in $id = $_GET['deleta']; will be empty because nothing is being sent, and consequently DELETE in mysql will do nothing.

Change the behavior of the button by sending the value of id via GET by redirecting via Javascript with onclick:

echo '<td><button onclick="location.href=\'deleta-cadastro.php?deleta='. $row['id_usr'] .'\'">Deleta</button>';

Now it seems to me that you are using the wrong variable in the query. Instead of $usuario should be $id and place the query between double quotes to insert the direct variable without concatenating:

$deleta = mysqli_query($conecta, "DELETE FROM `usuario` WHERE `id_usr` = $id";
  • Sam, now she can get the id from get. But there is no deletion in BD. it’s entering my loop "There was an error while deleting"

  • 1

    Thanks for the help Sam!!!!!!!!! It all worked out fine

Browser other questions tagged

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