0

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();
    })
  }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.