Helps to make a condition in php

5

I have here my php code so that you can delete the client, but only delete it when you write the client name (which is in the html). but I do not know how to make the condition if someone writes a name of a client that is not in the database. And I also do not understand when I enter a name atoa it the same as the deleted client message: /, does not else

   <script language="javascript">
<!--
function myFunction(a) {
    alert(a);
}

</script>

<?php

ini_set ('default_charset','utf-8');

$link = mysqli_connect("localhost", "root", "", "bd_calcadocharme");

$id=$_GET['id'];

$sql="DELETE FROM encomenda WHERE id='$id'";

$result = mysqli_query($link,$sql);

if ($result){
    echo "<script> myFunction('Cliente eliminado com sucesso'); </script>";
    header('refresh:0 ; url=pedir_rese.html'); }
else{
    echo "<script> myFunction('Erro ao tentar eliminar o registo na base de dados!'); </script>";
    header('refresh:0 ; url=pedir_rese.html');
}
?>
    
asked by anonymous 15.06.2015 / 00:19

2 answers

3

Your if is wrong, it only checks to see if the object exists, not how many rows have been modified.

In your case I would

$sql="DELETE FROM encomenda WHERE id='$id'";

if ($stmt = mysqli_prepare($link, $sql)) {

    mysqli_stmt_execute($stmt);

    if (mysqli_stmt_affected_rows($stmt) > 0)
    {
        mysqli_stmt_close($stmt);
        echo "<script> myFunction('Cliente eliminado com sucesso'); </script>";
        header('refresh:0 ; url=pedir_rese.html'); }
    }
    else
    {
        mysqli_stmt_close($stmt);
        echo "<script> myFunction('Erro ao tentar eliminar o registo na base de dados!'); </script>";
        header('refresh:0 ; url=pedir_rese.html');
    } 
}
One solution is to create a dropdown with only the existing clients in the database and it selects which one it wants to delete. Or change your message from Erro ao tentar eliminar o registo na base de dados! to Nenhum usuário com esse nome encontrado! .

    
15.06.2015 / 00:28
0

There are two options for you to use.

OPTION 1

You can perform a previous search to the database, verifying that the client exists. If the client exits, otherwise it shows the message that "error" occurred because it does not exist.

$sqlSelect = "SELECT id FROM encomenda WHERE id='$id'";
$data = [];
$result = false;

if($res = mysqli_query($link, $sqlSelect)) {
    while($return = mysqli_fetch_object($res)) {
        $data[] = $return;
    }
}

if(!empty($data)) {
    $sql = "DELETE FROM encomenda WHERE id='$id'";
    $result = mysqli_query($link, $sql);
}

if ($result){
    echo " myFunction('Cliente eliminado com sucesso'); ";
    header('refresh:0 ; url=pedir_rese.html'); 
} else {
    echo " myFunction('Erro ao tentar eliminar o registo na base de dados!'); ";
    header('refresh:0 ; url=pedir_rese.html');
}

OPTION 2

Check how many rows were affected in the last run, if it was greater than 0, then at least one record was deleted. In this case, your client has been deleted.

$sql = "DELETE FROM encomenda WHERE id='$id'";

mysqli_query($link, $sql);

if (mysqli_affected_rows($link) > 0){
    echo " myFunction('Cliente eliminado com sucesso'); ";
    header('refresh:0 ; url=pedir_rese.html'); 
} else {
    echo " myFunction('Erro ao tentar eliminar o registo na base de dados!'); ";
    header('refresh:0 ; url=pedir_rese.html');
}
    
20.05.2016 / 06:28