I have two table named: Blog for displaying blogs post and comments for displaying comments section for each blog post.
The fields for these are as follows:
For Blog:
1: blog_id
2: title
3: body
4: author
5: updated
For Comments:
1: comments_id
2: name
3: email
4: body
5: blog_id
The field blog_id must be the same for both the table.
Now what i want is that suppose for blog_id = “2”, I am writing comment, so what would be the insert query so that when i visit index.php?blog_id=2, I get the comments for only that particular post.
I mean how to insert the comments for a particular blog_id?
I tried this query to insert:
INSERT INTO cms.comments(blog_id, name,email,comments_body)
VALUES (
'".$arr['blog_id']."',
'".$arr['name']."',
'".$arr['email']."',
'".$arr['body']."'
) );
Ok, I have done it myself [NO need to implement foreign key].
Here is the solutions.
The requirement was that the field “blog_id” of comments must be equal to “blog_id” of blog
This can be done in the following manner. First get the blog’s “blog_id” by the following statement…
The using INSERT statement like this, insert it…
Now in order to retrieve it use the flowing query…
Using these queries, I have successfully implemented what I wanted…:)