尝试使用准备好的语句获取最后一个插入的ID。mysqli_insert_id始终返回零。
$sql = "INSERT INTO classes (id_course, id_organization, start_date, end_date, status, id_creator, assign_interval) VALUES (?, ?, ?, ?, ?, ?, 'monthly')";
if ($stmt = mysqli_prepare($link, $sql)) {
if (!mysqli_stmt_bind_param($stmt, "ssssss", $course, $idOrg, $thisClass["startDate"], $endDate, $thisClass["status"], $idUser)) {
echo "Error:could not bind parameters";
}
# CODE MISSING...
{
mysqli_stmt_execute($stmt);
$idClass = mysqli_insert_id($stmt);
}
}
else
{
echo 'ERROR: Could not prepare statement';
}
$sql = "INSERT INTO class_modules (id_class, id_module) select ?,id_module from modules m where m.id_course = ?";
if ($stmt = mysqli_prepare($link, $sql)) {
if (!mysqli_stmt_bind_param($stmt, "ss", $idClass, $course)) {
echo "Error:could not bind parameters";
}
if (!mysqli_stmt_execute($stmt)) {
echo "Error: could not update class modules:";
}
}
else
{
echo 'ERROR: Could not prepare statement';
}
拉莫斯之舞