关于mysql:PHP将值插入两个表中并将这些表中的键插入链接表中

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

我是PHP的新手,我感兴趣的是在链接表中的两个表中添加值之后,将两个表的主键ID(称为sub_credits(插入submit_id和credits_id))添加为外键,以此作为外键。任何帮助将是巨大的!这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
                    //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

1
2
3
4
5
6
7
8
9
10
11
12
13
$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