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.

remove-like.php

<?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.
forum.service.ts

  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.
topics.component.ts

  clickDislike() {     this.forumService.removeLike(this.dataService.getToken() + 'topic' + this.forumService.getLikeToken()).subscribe(result => {   this.ngOnInit();     })   } 

All answers to this question, which has the identifier 61064644

The best answer:

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.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data