Hendry
Hendry

Reputation: 87

DELETE FROM statement not working?

I've been trying to get my delete statement working.

This is how it should work: Whenever I press the delete button 'commentDelete' it should delete the comment, that has the commentID equal to the poster.

But instead, it only deletes the most previous comment, posted by the poster. I'm really confused, and can't figure out why. Here's my code I tried:

function commentsDelete($conn) {
  if(isset($_POST['commentsDelete'])){
    $commentID = $_POST['commentID'];

    $sql = "DELETE FROM comments WHERE commentID='$commentID'";
    $result = mysqli_query($conn, $sql);
    header("Location: commentpage.php");
  }
}

Upvotes: 1

Views: 56

Answers (2)

SolE-pAt21
SolE-pAt21

Reputation: 38

Remove the quotes from commentId if it is of numeric type in your database

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522161

The commentID is a integer

If the commentID column is numeric, then you should not be comparing against a quoted text string. Use this instead:

$sql = "DELETE FROM comments WHERE commentID=$commentID";

Upvotes: 4

Related Questions