Jump to content

insert data into database


johnhelen

Recommended Posts

Hello

 

I need to insert records into a table. A primary key will be generated automatically as it is serial number (postgres database). Then I have to take this key to insert into another table (as they have a relationship).

 

For example, I have to insert a record into movie table - this table has a primary key that is automatically created.

 

INSERT INTO movie (title, summary ) VALUES ('the ghost', 'a true story about ghost, 12 minutes');

 

After inser, I need to have get the movie_id

 

SELECT currval('movie_id_seq');

 

Then insert this id into a location_movie table. I have only one location_id, that is 27.

 

INSERT INTO location (movie_id, location_id) values (13, 27);

 

 

The problem here is that I have about 200 records and I have a list of INSERT queries like this

 

INSERT INTO movie (title, summary ) VALUES ('the ghost', 'a true story about ghost, 122 minutes');

INSERT INTO movie (title, summary ) VALUES ('titanic' 'a true story about titanic, 180 minutes');

INSERT INTO movie (title, summary ) VALUES ('the friend', '112 minutes');

....

....

 

How I create a php script that read each "INSERT" query, get the movie_id and add into location_movie table automatically using php?

 

Many thanks

shuhu

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.