Przede wszystkim sposób, w jaki masz zdefiniowane zmienne w chwili, gdy Author_ID2 nigdy nie jest zdefiniowany:
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle']));
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle_ID2']));
Tutaj po prostu zastępujesz $AuthorTitle
z wartością z $_POST['AuthorTitle_ID2']
i nie definiuj $AuthorTitle_ID2
w ogóle.
Następnie, instrukcja SQL, tak jak masz, łączyłaby tylko drugiego author
z study
w casestudy
ponieważ LAST_INSERT_ID()
zwróci ID ostatniego wstawionego rekordu tylko wtedy, gdy w rzeczywistości było kilka rekordów wstawionych przez jedną instrukcję.
I wreszcie, istnieje instrukcja IF w 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;
");