Verify that a record was deleted when executing DELETE query

5

I'm doing a simple PHP code to delete database email, but I need to use if and else if a given action occurs.

If, for example, deleting an email displays a certain message, or if it does not have the requested email in the database, a message appears that is no longer present in the table following the code.

<?php 

$emailDel = $_POST['delemail'];

$dbc = mysqli_connect('localhost', 'xavier', 'xavier', 'store_database')or die('não foi possivel acessar Banco de Dados');

$query = "DELETE FROM clients WHERE email = '$emailDel'";
$result = mysqli_query($dbc, $query)or die('não foi possivel acessar Banco de Dados');



if($result = TRUE){
    echo $emailDel.' as deleting from database';
}else{
    echo $emailDel.' it is not present in database';
}

mysqli_close($dbc);


?> 
    
asked by anonymous 19.10.2015 / 02:28

3 answers

3

Shortly after executing the query:

$result = mysqli_query($dbc, $query)or die('não foi possivel acessar Banco de Dados');

Check how many columns were affected with the mysqli_affected_rows() function.

The function mysqli_query() returns Boolean value.

1. true : When the execution was successful

2. false : when there was an error.

An important detail is that when you return true , it does not mean that some record has been deleted, but rather that the query was executed without problems.

So, after verifying the return of the query execution, check the return of mysqli_affected_rows() .

if ($result == true){
    if ($total = mysqli_affected_rows($dbc))
        echo 'excluído: '.$total;
    else
        echo 'Nenhum registro excluído';
else
   echo 'Houve erro na execuão da query SQL: '.mysqli_error($dbc);
    
19.10.2015 / 04:33
3

Try This $ success = mysql_affected_rows ();

if( isset($_POST['delemail']) ){   // Se existir o post prossiga... 
$emailDel = $_POST['delemail'];

$dbc = mysqli_connect('localhost', 'xavier', 'xavier', 'store_database')or    
die('não foi possivel acessar Banco de Dados');

$query = "DELETE FROM clients WHERE email = '$emailDel'";
$result = mysqli_query($dbc, $query)or die('não foi possivel acessar Banco   
de Dados');
$success = mysql_affected_rows();


if($success <> 0 ){ 
echo $emailDel.' as deleting from database';
}else{
echo $emailDel.' it is not present in database';
}

}else{    
echo"Erro";
}

mysqli_close($dbc);
    
19.10.2015 / 05:14
3

An equal = assignment operator means that the variable will receive a certain value.

Two equal equal to% condition operator.

Then the correct one is:

if($result == True){
    echo $emailDel.' as deleting from database';
} else {
    echo $emailDel.' it is not present in database';
}
    
19.10.2015 / 02:37