Вопрос

So I have a comments section on my website (blog style), so every post has a comments section.

I have a table 'posts' fields (post_id, isComment, title, timestamp, post etc.)

isComment is a boolean value which refers to if the post has any comments. If 0, no comments are searched for or shown, if 1, querys the table comments for comments for that post.

There's also a table 'comments' which has fields (comment_id, post_id, created etc..)

The post_id is the post the comment corresponds to.

Currently the query to remove a comment is:

 "REMOVE FROM comments WHERE comment_id = '$id';"

What I want to know, is if there is any way to find out if the comment deleted was the last comment corresponding to that post? And if so, then I would change the isComment value of that post to 0.

Это было полезно?

Решение

You can fire a query to find number of comments for that post, after every delete of a comment.

Something like.

select count(*) from comments where post_id = (select post_id from comments where comment_id='$id')

// You can optimize the query if u want.

Другие советы

Query for any comments of the given posts. If no rows are returned, reset your isComment flag with another query. (There might be a more efficient way to use the database to use its functions to calculate the number of rows and act accordingly.)

There are a couple ways to go about this. First, you could just count how many are left and act accordingly:

SELECT count(*) FROM comments WHERE post_id = '$id'

Or, if you already have the comment count when removing the comment, you can call mysql_affected_rows(...) or mysqli_affected_rows(...) to determine if something actually was deleted and how many.

Also, I believe in your original query, I believe your syntax should be DELETE instead of REMOVE?

DELETE FROM comments WHERE comment_id = '$id';
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top