Categories: php, mysql, sql, angular, typescript

Problem with the http delete function in Angular, no error

1 answer

I am trying to remove a record from my database. As of now the correct id is passing through as I can see in the Query String Parameters and I am not getting any errors. However, the row is not being deleted, im starting to think it is just a some syntax error but I am unsure.


<?php  $id=isset($_GET['id']) ? $_GET['id'] : die('ERROR: id not found.');  if($_POST) {  include 'connectPDO.php';  try {     $query = "DELETE FROM likes WHERE id = ?";      $stmt = $con->prepare($query);      $stmt->bindParam(1, $id);      $stmt->execute();  } catch (PDOException $exception) {     die('ERROR: ' . $exception->getMessage()); } } 

This is the service file where I am calling it.

  removeLike(id: string) {     return this.http.delete(`${this.baseUrl}/remove-like.php?id=${id}`);   } 

Then this is the part where I am calling that function in my component.

  clickDislike() {     this.forumService.removeLike(this.dataService.getToken() + 'topic' + this.forumService.getLikeToken()).subscribe(result => {   this.ngOnInit();     })   } 
Received answer to this question:
The best answer according to the author of the question:

You never enter inside your condition block, because if ($_POST) will always return false in your case. Indeed, you call this endpoint with an http DELETE request with an empty body.

<?php  $id=isset($_GET['id']) ? $_GET['id'] : die('ERROR: id not found.');  include 'connectPDO.php';  try {     $query = "DELETE FROM likes WHERE id = ?";      $stmt = $con->prepare($query);      $stmt->bindParam(1, $id);      $stmt->execute();  } catch (PDOException $exception) {     die('ERROR: ' . $exception->getMessage()); } 

If you want to make sure your endpoint is called with the right HTTP verb, please refer to this question, and add:

if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {      // Your code } 

Also, I recommend that you add a security mechanism if it's not already the case (e.g. authentication control), as a publicly available endpoint that deletes rows is a security issue.