Below I have an INSERT database function using php and mysqli:
for ($i = 1, $n = $_SESSION['sessionNum']; $i <= $n; $i++) {
$insertsql = "
INSERT INTO Session
(SessionName, SessionTime, SessionDate, SessionWeight, SessionDuration, TotalMarks, ModuleId, TeacherId, Room)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$sessname = $_SESSION['id'] . ($n == 1 ? '' : $i);
$sessdate = date("Y-m-d", strtotime($_SESSION['dateChosen']));
$insert->bind_param("sssisiiis", $sessname, $_SESSION['timeChosen'], $sessdate,
$_SESSION['totalWeight'], $time, $_SESSION['textMarks'],
$_SESSION['hiddenmodule'], $teacherid, $_SESSION['rooms']);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}
The above code will insert this data in the database below as an example in one go:
SessionId (Auto) SessionName SessionTime //etc
1 ADFGR1 01:00:00
2 ADFGR2 13:00:00
3 ADFGR2 09:00:00
Anyway you can see I have not include SessionId
in the code above as it is an auto increment meaning that it is going to be displayed anyway.
But the problem I have is that underneath this insert, what I want to do is that when the above insert is finished, I want to do another insert underneath:
$insertsession = "
INSERT INTO Session_Complete
(SessionId)
VALUES
(?)
";
if (!insertdb = $mysqli->prepare($insertsession)) {
// Handle errors with prepare operation here
}
insertdb->bind_param("i",$value);
insertdb->execute();
if (insertdb->errno) {
// Handle query error here
}
But the problem I have with the above insert is that it requires the already inserted SessionId
's from the first insert in order to insert those ids into its table. My question is how do I retrieve the SessionId
from the first insert so I can include them in the second insert?