Pertama-tama cara Anda mendefinisikan variabel Anda saat ini Author_ID2 tidak pernah didefinisikan:
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle']));
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle_ID2']));
Di sini Anda hanya mengganti $AuthorTitle
dengan nilai dari $_POST['AuthorTitle_ID2']
, dan jangan mendefinisikan $AuthorTitle_ID2
sama sekali.
Selanjutnya, pernyataan SQL seperti yang Anda miliki hanya akan menghubungkan author
kedua dengan study
dalam casestudy
karena LAST_INSERT_ID()
akan mengembalikan ID dari record yang terakhir disisipkan meskipun sebenarnya ada beberapa record yang disisipkan oleh satu pernyataan.
Dan terakhir, ada pernyataan JIKA di MySQL :
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle']));
$AuthorTitle_ID2 = mysqli_real_escape_string($con, ($_POST['AuthorTitle_ID2']));
$F_Name = mysqli_real_escape_string($con, ($_POST['first_name']));
$F_Name_ID2 = mysqli_real_escape_string($con, ($_POST['first_name_ID2']));
$L_Name = mysqli_real_escape_string($con, ($_POST['last_name']));
$L_Name_ID2 = mysqli_real_escape_string($con, ($_POST['last_name_ID2']));
$Email = mysqli_real_escape_string($con, ($_POST['email']));
$Email_ID2 = mysqli_real_escape_string($con, ($_POST['email_ID2']));
$Contactauthor = mysqli_real_escape_string($con, ($_POST['contact']));
$Contactauthor_ID2 = mysqli_real_escape_string($con, ($_POST['contact_ID2']));
$Title = mysqli_real_escape_string($con, ($_POST['Title']));
$Summary = mysqli_real_escape_string($con, ($_POST['Summary']));
mysqli_multi_query($con,"
START TRANSACTION;
INSERT INTO study(Title, Summary)
VALUES('$Title','$Summary');
SET @StudyId = LAST_INSERT_ID();
INSERT INTO author(AuthorTitle, F_Name, L_Name, Email, Contact)
VALUES('$AuthorTitle','$F_Name','$L_Name','$Email','$Contactauthor');
SET @AuthorId = LAST_INSERT_ID();
INSERT INTO casestudy(AuthorId, StudyId, Submitted)
VALUES(@AuthorId, @StudyId, NOW());
IF ('$AuthorTitle_ID2' <> '' OR '$F_Name_ID2' <> '' OR '$L_Name_ID2' <> '' OR '$Email_ID2' <> '' OR '$Contactauthor_ID2' <> '') THEN
INSERT INTO author(AuthorTitle, F_Name, L_Name, Email, Contact)
VALUES('$AuthorTitle_ID2','$F_Name_ID2','$L_Name_ID2','$Email_ID2','$Contactauthor_ID2');
SET @AuthorId = LAST_INSERT_ID();
INSERT INTO casestudy(AuthorId, StudyId, Submitted)
VALUES(@AuthorId, @StudyId, NOW());
END IF;
COMMIT;
");