0
我是新來的PHP和作爲測試我構建了一個測試應用程序加載存儲在數據庫中的問題,並將它們顯示到一個表格中,並將其顯示在一個帶有輸入類型單選按鈕的表格中供用戶選擇答案。有四張桌子。問題,答案,用戶,userexam。每個表中的行都包含一個id作爲索引。當我點擊提交按鈕時,我遇到的麻煩是將值輸入到數據庫中。我將如何循環並將每個問題值添加到數據庫中,而無需單獨定義每個ID?將字段值添加到數據庫
<?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;
?>
這是正確的嗎? $ sql =「INSERT INTO $ userexam(answerID,questionID,userID)VALUES('$ answer','$ question','$ student')」; –