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

时间:2016-05-16 10:26:02

标签: javascript php html mysql

我有这样的表格:

Form Image

提交表格后,个人详细信息将被插入到个人表格和书籍详细信息中。我想通过一个唯一的ID将一个个人表行链接到book表中的多行(因为它使用相同的查询插入),因此我可以确定这些书在以后与这个人相关。

这是我的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);

1 个答案:

答案 0 :(得分:1)

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

但您还必须在图书表中创建一个额外的列,用于存储用户的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);

}