Uncaught (in promise) RangeError: Maximum call stack size exceeded

Viewed 34

Here's my situation.

I have two directories on my project that stores files. One directory is for images as part of the body content from the summernote textarea and one for the actual attachment to which these attachments is also stored on my database table: tbl_ems_draft_attachments.

My images from summernote is stored on a directory and to the database table: tbl_ems_drafts as part of the body content, formatted as htmlentities(). I used preg_match_all() function to check if there's an image from the summernote, and I also check if there's an attachment to that particular draft so that if it is, once I press delete action, they'll get deleted from the database as well as from their corresponding directories.

However, if I press delete button, there's an error that says "jquery.min.js:2 Uncaught (in promise) RangeError: Maximum call stack size exceeded"

How can I achieve this? Thank you :)

Here's my source code below:

public function deleteDraftContent($post) {
    $data = [];
    $counter = 0;
    $fileCountRemoved = 0;
    $isSuccess = false;
    $sql = $this->query('SELECT * FROM tbl_ems_draft_attachments WHERE draft_id = "'.$post['draft_id'].'"');
    if ($this->num_rows($sql) > 0) {
      foreach ($sql as $row) {
        if (file_exists('../files/drafts/'.$row['file_name'])) {
          if (unlink('../files/drafts/'.$row['file_name'])) {
            $counter++;
            if ($counter === $this->num_rows($sql)) {
              $query = $this->query('SELECT * FROM tbl_ems_drafts WHERE draft_id = "'.$post['draft_id'].'"');
              if ($this->num_rows($query) > 0) {
                $row = $this->fetch($query);
                // this is to check if the body content has attached file
                if (preg_match_all("/[0-9]+\-[\w\s\d.-]+/", $row['body'], $matched)) {
                  for ($i = 0; $i < count($matched); $i++) {
                    if (file_exists('../files/summernote-img/'.$matched[$i])) {
                      if(unlink('../files/summernote-img/'.$matched[$i])) {
                        $fileCountRemoved++;
                        if ($fileCountRemoved === count($matched)) {
                          $deletion = $this->query('DELETE FROM tbl_ems_drafts WHERE draft_id = "'.$post['draft_id'].'"');
                          $isSuccess = ($deletion === true) ?? $isSuccess;
                        }
                      }
                    }
                  }
                } else {
                  $deletion = $this->query('DELETE FROM tbl_ems_drafts WHERE draft_id = "'.$post['draft_id'].'"');
                  $isSuccess = ($deletion === true) ?? $isSuccess;
                }
              }
            }
          }
        }
      }
    } 
    
    if ($this->num_rows($sql) === 0) {
      $query = $this->query('SELECT * FROM tbl_ems_drafts WHERE draft_id = "'.$post['draft_id'].'"');
      if ($this->num_rows($query) > 0) {
        $row = $this->fetch($query);
        // this is to check if the body content has attached file
        if (preg_match_all("/[0-9]+\-[\w\s\d.-]+/", $row['body'], $matched)) {
          for ($i = 0; $i < count($matched); $i++) {
            if (file_exists('../files/summernote-img/'.$matched[$i])) {
              if(unlink('../files/summernote-img/'.$matched[$i])) {
                $fileCountRemoved++;
                if ($fileCountRemoved === count($matched)) {
                  $deletion = $this->query('DELETE FROM tbl_ems_drafts WHERE draft_id = "'.$post['draft_id'].'"');
                  $isSuccess = ($deletion === true) ?? $isSuccess;
                }
              }
            }
          }
        } else {
          $deletion = $this->query('DELETE FROM tbl_ems_drafts WHERE draft_id = "'.$post['draft_id'].'"');
          $isSuccess = ($deletion === true) ?? $isSuccess;
        }
      }
    }
    return $isSuccess;
  }

Here's my button for my delete action:

$(document).on("click", "#btn-delete-draft-pane1", function (event) {
event.preventDefault();
// source code here...
});
0 Answers
Related