Check if a record was deleted when running DELETE query

Asked

Viewed 2,040 times

5

I’m making a simple PHP code to delete database email, only I need to use one if and else if a given action occurs.

If for example, deleting an email shows a particular message, or if you do not have that email requested in the database, a message appears that this data is no longer present in the table follows 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);


?> 
  • Missed fetch_assoc, without it you do not take the result of the query

  • This is wrong if($result = TRUE){ right is if($result == TRUE){ or if($result === TRUE){ or just if($result){.

  • @rray I think this is not necessary because it is not a consultation but a DELETE from?

  • What’s the matter?

  • 1

    I found the problem quite clear.. I did not understand why they voted negative or to close.

  • @rray because the consultations of the type DELETE, UPDATE, INSERT return TRUE or FALSE only.

  • @Edilson, there was no code before in the question, I figured I was doing a select before delete.

  • @rray Yes, I just saw that the question was edited.

Show 3 more comments

3 answers

3

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

Two of a kind == condition operator.

So the right thing is:

if($result == True){
    echo $emailDel.' as deleting from database';
} else {
    echo $emailDel.' it is not present in database';
}
  • yeah, I’ve seen it here and how I’m new to php, now that I’ve seen the question of == for tbm condition extends to it, but that same tbm n worked, is that getting an answer to identify whether it has the value in the table for php to confirm would not be possible ?

  • It must be an error before this condition, exprerimente put it at the beginning of your PHP file and edit your code with the error that returned: ini_set('display_errors', 'On'); and just below error_reporting(E_ALL);.

  • thanks, but I tried here, but nothing was identified.

  • It has the code in the question, I edited it, I do not know if it was approved

  • I approved, the code is there yes.

  • $result receives the return of mysqli_query(), which does not mean that any record has even been deleted because mysqli_query() returns true or false... if true, indicates that the query was executed without expected error, even if nothing was deleted.

Show 1 more comment

3

Right 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 function mysqli_affected_rows().

The function mysqli_query() returns boolean value.

1. true: when the execution was successful

2. false: when there was a mistake.

An important detail is that when you return true, does not mean that some record was deleted, but rather that the query was executed smoothly.

Therefore, just after checking 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);

3

Try So
$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);
  • <> 0 is different from != 0.

  • That was a question?

  • A statement... because a question has question.. Mr. President... just a warning about what you used on parole.. It is wrong, although it does not generate any error. Only semantically incorrect..

Browser other questions tagged

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