将字段值添加到数据库

I'm new to php and as a test I built a test taking application that loads the questions stored in a database and displays them into a table with input type radio buttons for the user to select the answer. There are four tables. Questions, answers, users, userexam. The rows in each table contain an id as an index. What I'm having trouble with is imputing the values into the database when the submit button is clicked. How would I loop through and add each questions value into the database without defining each id individually?

 <?php

$getQuestions = "
    SELECT
        *

    FROM
        questions
    WHERE questions.active = '1'

    ORDER BY
        questionID
";
$qResult = mysql_query($getQuestions);

$tableString = "<table>
                    <tr>
                        <th>
                            Questions
                        </th>
                        <th>
                            Answers
                        </th>
                    </tr>";

while ($qRow = mysql_fetch_assoc($qResult)) {
    $getAnswers = "
        SELECT
            *
        FROM
            answers a
        WHERE
            a.questionID = '" . $qRow['questionID'] . "'
        ORDER BY
            a.questionID,
            a.answerNumber
    ";
    $aResult = mysql_query($getAnswers);

    $tableString .= "<tr>
                        <td>" .
                            $qRow['question'] .
                        "</td>
                        <td>";

    while ($aRow = mysql_fetch_assoc($aResult)) {
        if ($aRow['correct'] == 1) {
            $tableString .= "<input name=". $qRow['questionID'] ."
                                type='radio'
                            >" .
                                $aRow['answerValue'] . "<br />";
        } else {

            $tableString .= "<input name=". $qRow['questionID'] ."
                                type='radio'
                            >" .
                                $aRow['answerValue'] . "<br />";
        } 
        $answer=$_POST['. $aRow["answerID"] .'];
        $question=$_POST['. $qRow["questionID"] .'];
        $student=$_POST['userID'];

        // Insert data into mysql 
        $sql="INSERT INTO $userexam(answerID, questionID, userID)VALUES('$answer', '$question', '$student')";
        $result=mysql_query($sql);
    }


}
$tableString .= "</table>";
echo $tableString;
 ?>

Use mysqli(mysql-improved) in every places of mysql.

    <?php
//Set database conection
$conection=mysqli_connect('localhost','root','','Your database name');
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$getQuestions = "
    SELECT
        *

    FROM
        questions
    WHERE questions.active = '1'

    ORDER BY
        questionID
";
$qResult = mysqli_query($conection,$getQuestions);

$tableString = "<table>
                    <tr>
                        <th>
                            Questions
                        </th>
                        <th>
                            Answers
                        </th>
                    </tr>";

while ($qRow = mysqli_fetch_assoc($qResult)) {
    $getAnswers = "
        SELECT
            *
        FROM
            answers a
        WHERE
            a.questionID = '" . $qRow['questionID'] . "'
        ORDER BY
            a.questionID,
            a.answerNumber
    ";
    $aResult = mysqli_query($conection,$getAnswers);

    $tableString .= "<tr>
                        <td>" .
                            $qRow['question'] .
                        "</td>
                        <td>";

    while ($aRow = mysqli_fetch_assoc($aResult)) {
        if ($aRow['correct'] == 1) {
            $tableString .= "<input name=". $qRow['questionID'] ."
                                type='radio'
                            >" .
                                $aRow['answerValue'] . "<br />";
        } else {

            $tableString .= "<input name=". $qRow['questionID'] ."
                                type='radio'
                            >" .
                                $aRow['answerValue'] . "<br />";
        } 
        $answer=$_POST['. $aRow["answerID"] .'];
        $question=$_POST['. $qRow["questionID"] .'];
        $student=$_POST['userID'];

        // Insert data into mysql 
        $sql="INSERT INTO userexam(answerID, questionID, userID)VALUES('$answer', '$question', '$student')";
        $result=mysqli_query($conection,$sql);
    }


}
$tableString .= "</table>";
echo $tableString;
mysqli_close($conection);
 ?>