954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to insert the rows in the database mysql table in the following?

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: username

3: email

4: comment_body

5: blog_id

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['comments_body']."'
                ) WHERE cms.blog.blog_id = '$cms.blog.blog_id'");


I tried the foreign key concept : SELECT @last := LAST_INSERT_ID(), but coud not understand how to use this.

amityadav9314
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

To insert the comment you'll have to know to which blog entry it belongs.
To insert a comment for entry with id=2, use:

INSERT INTO cms.comments(blog_id, name,email,comments_body) VALUES (2, 'theName', 'theMail', 'theComment' );

You cannot use a WHERE clause in an INSERT statement.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

You can't use a WHERE clause with an INSERT statement. You are inserting a new row so matching on a WHERE clause makes no sense. As a blog can have many comments searching in the comments table by blog_id isn't a good idea anyway (except for SELECT of course but you wouldn't want to do that with an UPDATE).

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

But that was for just blog_id = 2, what if I want to insert blog_id as a variable?
May be something like this: $blog_id ?

amityadav9314
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

To insert the comment you'll have to know to which blog entry it belongs. To insert a comment for entry with id=2, use:

INSERT INTO cms.comments(blog_id, name,email,comments_body) VALUES (2, 'theName', 'theMail', 'theComment' );

You cannot use a WHERE clause in an INSERT statement.


But that was for just blog_id = 2, what if I want to insert blog_id as a variable?
May be something like this: $blog_id ?

amityadav9314
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Your blog_id will need to be inserted, most probably from a variable that you have stored on the page. The name you give it is up to you. We were both just saying that your SQL statement was incorrect in including a WHERE clause.

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

rivate Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


Call ImageRetrieve()
End sub

Private Sub ImageRetrieve()
imgsave2.Update()
Dim OleDbConnection1 As New System.Data.OleDb.OleDbConnection("PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=D:\Testing\DATABASE\TESTING.MDB;PERSIST SECURITY INFO=FALSE;")
OleDbConnection1.Open()
Dim OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()
Dim OleDbReader1 As System.Data.OleDb.OleDbDataReader

'Access (OleDb)
OleDbSelectCommand1.CommandText = "Select h1_img,h2_img,h3_img,h1_sign,h2_sign,h3_sign from vLOCKER WHERE lockerno = " & TextBox1.Text & ""
OleDbSelectCommand1.Connection = OleDbConnection1

' RUN THE COMMAND
OleDbReader1 = OleDbSelectCommand1.ExecuteReader()
OleDbReader1.Read()
Dim Len1 As Long = OleDbReader1.GetBytes(0, 0, Nothing, 0, 0)
Dim Array1(CInt(Len1)) As Byte
OleDbReader1.GetBytes(0, 0, Array1, 0, CInt(Len1))

OleDbConnection1.Close()

Dim MemoryStream1 As New System.IO.MemoryStream(Array1)
Dim Bitmap1 As New System.Drawing.Bitmap(MemoryStream1)
imgsave2.Image = Bitmap1
End Sub
image retrieve from access database but can"t loop in database

SANJAY26
Newbie Poster
10 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: