gpt4 book ai didi

javascript - 将表的一行链接到 MySQL 中另一个表中的多行

转载 作者:行者123 更新时间:2023-11-28 01:12:04 26 4
gpt4 key购买 nike

我有这个表格:

Form Image

提交表单后,个人详细信息将插入个人表,图书详细信息将插入图书表。我想通过一个唯一的 id 将一个个人表行链接到书表中的多行(因为它是用相同的查询插入的),这样我可以在以后识别这些书与这个人有关。

这是我的 PHP 和 MySQL 脚本:

$mysqli = new mysqli($host,$user,$password,$database);

$users_firstname = $_POST['firstname'];
$users_middlename = $_POST['middlename'];
$users_lastname = $_POST['lastname'];
$users_gender= $_POST['gender'];
$users_location= $_POST['location'];
$users_email= $_POST['email'];
$users_mobile= $_POST['mobile'];

$query = "INSERT INTO personaldetails(FirstName ,MiddleName,LastName,
Gender,Location,Email,Mobile) VALUES ('$users_firstname',
'$users_middlename', '$users_lastname', '$users_gender','$users_location','$users_email','$users_mobile');";

foreach($_POST['booktitle'] as $key => $bookTitle) {
$bookTitle = mysqli_real_escape_string($mysqli, $bookTitle);
$bookGenre = mysqli_real_escape_string($mysqli, $_POST['bookgenre'][$key]);
$bookWriter = mysqli_real_escape_string($mysqli, $_POST['bookwriter'][$key]);
$bookDescription = mysqli_real_escape_string($mysqli, $_POST['bookdescription'][$key]);

$query .= "INSERT INTO bookdetails(BookTitle ,BookGenre,BookWriter,
BookDescription) VALUES('$bookTitle',
'$bookGenre', '$bookWriter', '$bookDescription');";
}

$result = mysqli_multi_query($mysqli, $query);

最佳答案

一种可能的解决方案是使用 mysqli_insert_id .

但是您还必须在book 表 中创建一个额外的列来存储用户的id,以便将一本书与用户相关联,让它成为user_id(它将存储我们将通过 mysqli_insert_id() )

获得的新创建用户的 ID

并且您还必须单独执行查询以获取新插入的用户的 ID。

所以代码会像-

$mysqli = new mysqli($host,$user,$password,$database);

$users_firstname = $_POST['firstname'];
$users_middlename = $_POST['middlename'];
$users_lastname = $_POST['lastname'];
$users_gender= $_POST['gender'];
$users_location= $_POST['location'];
$users_email= $_POST['email'];
$users_mobile= $_POST['mobile'];

$user_query = "INSERT INTO personaldetails(FirstName ,MiddleName,LastName,
Gender,Location,Email,Mobile) VALUES ('$users_firstname',
'$users_middlename', '$users_lastname', '$users_gender','$users_location','$users_email','$users_mobile');";

//execute the user query
$result = mysqli_query($mysqli, $user_query);
//get the user id of newly inserted user
$user_id = mysqli_insert_id($mysqli);

foreach($_POST['booktitle'] as $key => $bookTitle) {
$bookTitle = mysqli_real_escape_string($mysqli, $bookTitle);
$bookGenre = mysqli_real_escape_string($mysqli, $_POST['bookgenre'][$key]);
$bookWriter = mysqli_real_escape_string($mysqli, $_POST['bookwriter'][$key]);
$bookDescription = mysqli_real_escape_string($mysqli, $_POST['bookdescription'][$key]);

//use the user id here to relate it with the book
$book_query = "INSERT INTO bookdetails(BookTitle ,BookGenre,BookWriter,
BookDescription, user_id) VALUES('$bookTitle',
'$bookGenre', '$bookWriter', '$bookDescription', '$user_id');";
//execute the query for book
$result = mysqli_query($mysqli, $book_query);

}

关于javascript - 将表的一行链接到 MySQL 中另一个表中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com