Error executing and showing deletion of an id in the database using PHP

Asked

Viewed 33 times

-3

I am having difficulty executing the deletion of a person in the Mysql database using ID, but it does not perform the deletion as the ID entered for deletion still remains in the database and does not show on the page in PHP.

<!DOCTYPE html>
<html>
<head>
    <title> Excluir </title>
</head>
<body>
    <form method="GET" action="excluir_cliente.php">
        <p> Nome do ID: <input type="text" name="id_cliente" size=30> </p>
        <p> <input type="submit" value="Eliminar"> </p>
        <p> <a href="aluno.php"> Formulário aluno </a>
    </form>
</body>
</html>
<html>
<head> 
    <title> Remover </title> 
</head>
<body> 
    <h2> Remover cliente </h2>
    <?php
        $codrem = $_GET['id_cliente'];
        if (!$codrem) {
            echo 'Volte atrás e escreva o código do cliente a remover.'; 
        }
        echo "Cliente a remover: $codrem. <p>";
        $ligax = mysqli_connect('localhost', 'root','');
        if (!$ligax){
            echo "<p> Falha na ligação."; exit; 
        }
        mysqli_select_db($ligax, 'aluno');
        $consulta = "SELECT * FROM cliente";
        $result = mysqli_query($ligax, $consulta);
        $nr_antes = mysqli_num_rows($result);
        $remove = "DELETE FROM cliente WHERE id_cliente = '%$codrem%'";
        $result = mysqli_query($ligax, $remove);

        if ($result==0) echo "<p> Não removido <br>";

        $consulta = "SELECT * FROM cliente";
        $result = mysqli_query($ligax, $consulta);
        $nr_depois = mysqli_num_rows($result);
        $nr_removidos = $nr_antes - $nr_depois;
        echo 'Nº de registos removidos: '.$nr_removidos;
    ?>
<p> <a href="listar.php"> Listar registos </a>
</body> 
</html> 

2 answers

1

There is a feature that perform this work for you, do not need to pick the difference to subtract and get one results. The function to be used is this:

 mysqli_affected_rows($ligax)

So your html should look like this:

<html>
<head> 
    <title> Remover </title> 
</head>
<body> 
    <h2> Remover cliente </h2>
    <?php
        $codrem = $_GET['id_cliente'];
        if (!$codrem) {
            echo 'Volte atrás e escreva o código do cliente a remover.'; 
        }
        echo "Cliente a remover: $codrem. <p>";
        $ligax = mysqli_connect('localhost', 'root','');
        if (!$ligax){
            echo "<p> Falha na ligação."; exit; 
        }
        $remove = "DELETE FROM cliente WHERE id_cliente = '%$codrem%'";
        $result = mysqli_real_query($ligax, $remove);
        if ($result == FALSE) echo "<p> Não removido <br>";
        else echo 'Nº de registos removidos: '. mysqli_affected_rows($ligax);
    ?>
<p> <a href="listar.php"> Listar registos </a>
</body> 
</html> 
  • It didn’t work out and it’s showing off Não removido.

  • actually occurred to delete in the database, because looking at your delete this wrong place %

  • Another thing q might be is its mysqli_query function, it is used for SELECT, SHOW, DESCRIBE or EXPLAIN and not for delete. You can replace it with mysqli_real_query($ligax, $remove), which will return TRUE and FALSE.

1

Analyzing your code:

$remove = "DELETE FROM cliente WHERE id_cliente = '%$codrem%'";

That part seems to be the problem:

'%$codrem%'

if that $codrem is a variable with the id the correct would be:

$remove = "DELETE FROM cliente WHERE id_cliente = ".$codrem;
  • The problem has been solved and the desired ID has been deleted, obg.

Browser other questions tagged

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