D
D
Dmitry2017-10-14 02:56:55
PHP
Dmitry, 2017-10-14 02:56:55

mysqli_affected_rows query returns 0 on DELETE?

There is a request:

DELETE FROM images WHERE id_news='$id_news' AND im_path='$image'

the mysqli_affected_rows check returns 0, but actually records are deleted from the database as expected. Why is that?
Here is the complete piece of code:
public function delete($query){	
  $result = mysqli_query($this->link, $query);
            
  if (!$result){
    $array_answer[0] = false;
    $array_answer[1] = mysqli_error($this->link);
  } else if(!mysqli_affected_rows($this->link)){
    $array_answer[0] = false;
    $array_answer[1] = "Не удалено ни одной записи!";
    $array_answer[2] = mysqli_affected_rows($this->link);
  } else{
    $array_answer[0] = true;
  }

  return $array_answer;	
}

$query = "DELETE FROM images WHERE id_news='$id_news' AND im_path='$image'";
$result = $this->msql->delete($query);

I found the problematic piece of code:
foreach ($remove_imgs as $image) {
      $query = "DELETE FROM images WHERE id_news='$id_news' AND im_path='$image'";
      $result = $this->msql->delete($query);
      if(!$result[0]){
        //$answer = "Часть изображений не удалена, обновите страницу|";
        $answer = $result;
      }
     }

the records are deleted, but the return of the results is not correct. If you remove the cycle then it starts to work correctly. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-10-14
@xDMITRIYx

I got out of the situation by rebuilding the query using the IN operator

for($i = 0, $l = count($remove_imgs); $i < $l; $i++){
      $remove_imgs[$i] = '\'' . $remove_imgs[$i] . '\'';
    }
    $string_remove_imgs = implode(',', $remove_imgs);
  
    $query = "DELETE FROM images WHERE id_news='$id_news' AND im_path IN ($string_remove_imgs)";

so it works well. But still, tell me, please - why did this request not work in the cycle?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question