我是PHP的新手,我感兴趣的是在链接表中的两个表中插入值之后,将两个表的主键ID(称为sub_credits(插入submit_id和credits_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.');
最佳答案
您可以使用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
通过新的插入查询插入到新表中。$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
是新表的外键关于php - PHP将值插入两个表并将这些表的键插入链接表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36819905/