简体   繁体   English

如何更新两个mySQL表并将主键从第一个表输入到第二个表?

[英]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: 就所有数据一次提交而言,这是一种快速算法:

  1. Insert your video record 插入您的视频记录
  2. Get the last insert id 获取最后的插入ID
  3. Loop over your video meta data and insert those records setting the foreign key from step 2. 循环播放视频元数据,并插入设置第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. 不能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.

相关问题 MySQL将一个表的主键添加到其他两个表 - Mysql add primary key from a table to others two tables mysql使用第二张表中的auto_increment值作为第二张表中的外键插入到两个表中 - mysql insert into two tables using auto_increment value from the first table as a foreign key in the second Mysql在两个表中插入其中第一个表具有外键并更新第一个表 - Mysql insert in two tables where the first table has foreign key and update the first table 根据第一个表中的主键插入到多个 mysql 表 - Inserting to multiple mysql table based on an primary key from first table 如何从两个表(如一个表的所有行)以及相对于其他表的第一个表的主键总和中获取数据 - How to get data from two tables like one table all rows and with respect to first table primary key sum of row from other table 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 如何从PHP MySQL中的两个不同表更新表? - How to update table based from two different tables in php MySQL? 如何更新MySQL两列查找表,其中每个字段都是主键 - How to update MySQL two column lookup table where each field is Primary Key 如何从两个mysql表中选择数据,显示第一个表中的数据,并使用弹出框显示第二个表中的数据 - How do I select data from two mysql tables, display the data from the first table and use a popover to show data from the second table mysql - 如何在没有主键的情况下更新表中的特定行? - How to update a specific row in a table without a primary key in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM