我是PHP的新手,我有兴趣在这些表中插入值之后立即从名为sub_credits的链接表(插入submit_id和credits_id)中的两个表中添加主键ID作为外键。任何帮助都会很棒!这是我的代码:
//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.');
答案 0 :(得分:0)
您可以使用 mysql_insert_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
使用新的插入查询插入新表。
$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
是新表的外键