Jump to content

SQL INSERT Help


Flukey

Recommended Posts

Hey guys,

I have a table called tbl_article with the following fields:
article_id > autonumber > primary key
article_hits > int
article_approved > boolean
article_image > boolean

and another table called tbl_links with the following fields:

link_id > autonumber > primary key
genre_id > number
user_id > number
link_headline > text
link_description > memo
link_url > text
article_id > number
link_votes > number
link_score > number
link_hits > number
link_date > number

tbl_article:article_id has a one-to-one relationship with tbl_link:article_id

Now, 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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.html

and then this one afterwards:

http://dev.mysql.com/doc/refman/4.1/en/insert-select.html

Regards
Huggie
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.