Вот как может выглядеть рабочий пример вашего кода (с некоторыми из предложенных изменений из комментариев). Я сохранил ваш оригинальный код в основном как есть, но изменил расположение и порядок в вашем скрипте.
Разделение HTML и PHP - одна из вещей, к которой вы всегда должны стремиться. Как только все PHP-код будет ближе друг к другу, удалите дубликаты или ненужные вещи (включая лишний запрос SELECT для последнего вставленного идентификатора).
Я добавил несколько комментариев для дополнительного объяснения. Остальное в основном нетронутым =)
<?php
// first - all code that should always be executed,
// this will later be used in the html output.
$showAllClasses = "SELECT * FROM class";
$showClassesResult = $mysqli->query($showAllClasses) or die ('Error finding Classes');
// now the code that should only be executed on POST request
if (isset($_POST['submit'])) {
// we only want things to happen if all queries are successful,
// otherwise we end up with quizzes without class connections.
$mysqli->begin_transaction();
//Get POST variables
$quizTitle = '"' . $mysqli->real_escape_string($_POST['quizTitle']) . '"';
$description = '"' . $mysqli->real_escape_string($_POST['description']) . '"';
//Question query
$quizCreationQuery = "INSERT INTO quiz (quizTitle, description) VALUES($quizTitle, $description)";
$mysqli->query($quizCreationQuery) or die($mysqli->error . __LINE__);
// The $mysqli instance knows the last inserted id, so we can just use that.
$insertedQuizId = $mysqli->insert_id;
foreach ($_POST['check_box'] as $classID) {
$ClassQuizQuery = "INSERT INTO quiz_class(classID, quizID) VALUES ('$classID', $insertedQuizId)";
$mysqli->query($ClassQuizQuery) or die($mysqli->error . __LINE__);
}
// Everything should have worked so we can now commit the transaction
$mysqli->commit();
}
// now that we are done with everything, we start with the html output.
// since we have done all the complicated stuff above, all we have to care
// about, is the html output and iterating over our classes to create the html table.
?>
<form method="post" action="#">
<p>
<label>Quiz Title: </label>
<input type="text" placeholder="Insert Quiz Title here" name="quizTitle" class="form-control"/>
</p>
<p>
<label>Quiz Description: </label>
<input type="text" placeholder="Insert Quiz Description here" name="description" class="form-control"/>
</p>
<table border='1' cellpadding='10' align='center'>
<tr><th></th><th>Class ID</th><th>Class Name</th><th>Class Description</th></tr>
<?php while ($row = $showClassesResult->fetch_object()): ?>
<tr>
<td><input type='checkbox' id='<?= $row->classID ?>' name='check_box[]' value='<?= $row->classID ?>'></td>
<td><?= $row->classID ?></td>
<td><?= $row->className ?></td>
<td><?= $row->classDesc ?></td>
<td><button type='button' name='add' id='add' data-toggle='modal' data-target='#questionType' class='btn btn-success'>Edit Students</button></td>
</tr>
<?php endwhile ?>
</table>
<div align="center">
<input type="submit" name="submit" value="Submit" class="btn btn-info"/>
</div>
</form>