[英]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. 这些字段是使用jquery动态添加或删除的。
I'd like to submit all this information 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']); if(isset($ _ POST ['submit'])){$ myvideo-> addVideo($ sequence,$ _FILES ['fileName'] ['name'],$ _POST ['vidTitle'],$ _POST ['vidCat' ],$ _FILES ['thumbName'] ['name']);
} else { echo $myvideo->error; } 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); $ 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. 我希望将其放入与“ addvideo”相同的if语句中,并希望使用第一次插入时的主键作为外键进行更新。 how can I accomplish this?` 我怎样才能做到这一点?
Check out mysql_insert_id() . 检出mysql_insert_id() 。 It retrieves the most recent insert id of the current connection. 它检索当前连接的最新插入ID。
As far as a quick algorithm if all the data were submitted at one time: 就所有数据一次提交而言,这是一种快速算法:
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. 不能100%确定这是否满足您一次完成所有操作的愿望。
Here is a link to some information on setting Foreign Keys using PHP: http://www.codingforums.com/archive/index.php/t-161945.html 以下是有关使用PHP设置外键的一些信息的链接: http : //www.codingforums.com/archive/index.php/t-161945.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.