PHP 将值插入两个 table 并将这些 table 中的键插入 link table

PHP Insert values into two tables and insert keys from those tables into link table

我是 PHP 的新手,我有兴趣在名为 sub_credits 的 link table 中添加来自两个 table 的主键 ID (插入 submit_id 和 credits_id)作为在 table 中插入值后的外键。任何帮助都会很棒!这是我的代码:

                    //write the data to the database
                    $query1 = "INSERT INTO submissions (user_id, date, " . 
                             "submission_type, title, zipfile, " . 
                             "copyright, review)" . 
                             "VALUES ('" . $_SESSION['user_id']. "', NOW(), " . 
                             "'$submission_type', '$title', " . 
                             "'$zipfile', '$copyright', '$review' )";
                    $query2 = "INSERT INTO credits (credits) VALUES ('$credits')";
                    mysqli_query($dbc, $query1)
                            or die ('Data not inserted.');
                    mysqli_query($dbc, $query2)
                            or die ('Data not inserted.');

您可以使用 mysql_insert_id();

获取 ID
$query1 = "INSERT INTO submissions (user_id, date, " . 
                         "submission_type, title, zipfile, " . 
                         "copyright, review)" . 
                         "VALUES ('" . $_SESSION['user_id']. "', NOW(), " . 
                         "'$submission_type', '$title', " . 
                         "'$zipfile', '$copyright', '$review' )";
mysqli_query($dbc, $query1) or die ('Data not inserted.');
$id1 = mysql_insert_id();

$query2 = "INSERT INTO credits (credits) VALUES ('$credits')";

mysqli_query($dbc, $query2) or die ('Data not inserted.');
$id2 = mysql_insert_id();

您可以使用 $id1$id2 使用新的插入查询插入到新的 table。

$query3 = "INSERT INTO new_table(submissions_id,credits_id) VALUES ('$id1','$id2')";
mysqli_query($dbc, $query3) or die ('Data not inserted.');

其中 submissions_id,credits_id 是新 table

外键