How to update two mySQL tables and input primary key from first table into second table?
My first table contains all sorts of info about videos such as: video title, video thumbnail, video category, etc. My second table will store the key points of each video.. So one video may have three key points for example. These fields are added or deleted dynamically using jquery.
I'd like to submit all this informat开发者_运维知识库ion at once, and I'd like to link the videos and their key points using the video's primary key.
Alternatively I could link them using the video's file name or something like that. But it seems sloppy to do it that way.
here is some code:
if (isset($_POST['submit'])) { $myvideo->addVideo ($sequence, $_FILES['fileName']['name'], $_POST['vidTitle'], $_POST['vidCat'], $_FILES['thumbName']['name']);
} else { echo $myvideo->error; } }this method is adding a video to the database.
i will probably introduce a method that adds the key points to the database like so:`
$myvideo->addPoints($keypoint, $minutes, $seconds);
I want this to go inside the same if statement as "addvideo" and I want this to be updated with the primary key from the first insert as a foreign key. how can I accomplish this?`
Check out mysql_insert_id(). It retrieves the most recent insert id of the current connection.
As far as a quick algorithm if all the data were submitted at one time:
- Insert your video record
- Get the last insert id
- Loop over your video meta data and insert those records setting the foreign key from step 2.
If I am understanding correctly, you may want to consider creating a primary key on the video record and then use that as a Foreign Key in the second table. Not 100% certain if this satisfies your wish to do it all at once.
Here is a link to some information on setting Foreign Keys using PHP: http://www.codingforums.com/archive/index.php/t-161945.html
精彩评论