Inserting data and retrieving data???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Inserting data and retrieving data???

Post by cturner »

I am wondering if someone could please tell me how I can insert an article into the items table then insert an articleid into the comments table and retrieve the articleid when viewing the comments for that article with PHP and MySQL? I have been trying to work it out but am getting nowhere. Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The basic concept is to use an INSERT query on the items table to produce the articleID for later use. Afterwhich you use your database's facility to retrieve the last primary key id generated and use that as an INSERT query against the comments table.

Pulling out the item is a fairly simple inner join.

Code: Select all

SELECT
  *
FROM
  itemTable
INNER JOIN
  commentTable
ON
  commentTable.articleID = itemTable.articleID
WHERE
  commentTable.commentID = '2'
Post Reply