Flukey Posted October 17, 2006 Share Posted October 17, 2006 Hey guys,I have a table called tbl_article with the following fields:article_id > autonumber > primary keyarticle_hits > intarticle_approved > booleanarticle_image > booleanand another table called tbl_links with the following fields:link_id > autonumber > primary keygenre_id > number user_id > numberlink_headline > textlink_description > memolink_url > textarticle_id > numberlink_votes > numberlink_score > numberlink_hits > numberlink_date > numbertbl_article:article_id has a one-to-one relationship with tbl_link:article_idNow, when it comes to the insert statement, how do i do it so i insert a new article_id and field values in tbl_article. But also, get the new article id and put it into tbl_links.Is there one long statement i could use?Would i have to insert the record into tbl_article, find the article_id and then insert a record into tbl_link?I hope this makes sense.Thanks guys.Jamie. Quote Link to comment https://forums.phpfreaks.com/topic/24197-sql-insert-help/ Share on other sites More sharing options...
marcus Posted October 17, 2006 Share Posted October 17, 2006 id's are auto increments, they will automatically increase each time you make a new row.[code]$query = "INSERT INTO tbl_article (`article_hits` , `article_approved` , `article_image` ) VALUES ('$hits' , '$approved' , '$image');";$result = mysql_query($query);$query = "INSERT INTO tbl_links (`genre_id` , `user_id` , `link_headline` , `link_description` , `link_url` , `article_id` , `link_votes` , `link_score` , `link_hits` , `link_date`) VALUES ('$gid' , '$uid' , '$hdln' , '$desc' , '$url' , '$aid', '$votes' , '$score' , '$hits', '$date' );";$result = mysql_query($query);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24197-sql-insert-help/#findComment-109963 Share on other sites More sharing options...
Flukey Posted October 17, 2006 Author Share Posted October 17, 2006 Yes, i know that.But that wasn't my question.I'm relating ArticleIDs. I would like to know how to do a SQL statement, so once i add a new record to tbl_article i can relate that article_id to tbl_link.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/24197-sql-insert-help/#findComment-109965 Share on other sites More sharing options...
HuggieBear Posted October 17, 2006 Share Posted October 17, 2006 I think that Jamie meant that he wanted to do it in one statement as opposed to two...This can be done in Oracle, I believe it can probably be done in MySQL too. Check this page out:http://dev.mysql.com/doc/refman/4.1/en/insert.htmland then this one afterwards:http://dev.mysql.com/doc/refman/4.1/en/insert-select.htmlRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24197-sql-insert-help/#findComment-109966 Share on other sites More sharing options...
Flukey Posted October 17, 2006 Author Share Posted October 17, 2006 Cheers HuggieBear.I've just done this SQL, and it works like a treat :P[code=php:0]$sql = "INSERT ALL INTO tbl_article (article_headline, article_body, article_date, article_hits, article_approved) VALUES ('$headline', '$body', ".time().", 0, 1) INTO tbl_link (link_headline, link_description, genre_id);";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24197-sql-insert-help/#findComment-109967 Share on other sites More sharing options...
HuggieBear Posted October 17, 2006 Share Posted October 17, 2006 Glad to have been of some assistance... I'll put those Oracle books back to the bottom of the dust pile now ;)Huggie Quote Link to comment https://forums.phpfreaks.com/topic/24197-sql-insert-help/#findComment-109974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.