我有點難倒在這裏...需要幫助通過調查答覆從HTML表單MySQL表
我有一個網頁,顯示了存儲在一個數據庫表的調查問題清單,我試圖收集每個問題的評分回答(強烈反對 - >非常同意),並將這些值傳回我的數據庫中的另一個表。
這裏的表單代碼的一個片段:
<form action="submitAnswers.php" name="subAns" method="post">
<fieldset>
<legend>Survey Questions</legend>
<input type="hidden" name="survey_id" value="<?php echo ($survey_id); ?>">
<table width=90% border=0 cellpadding='2' cellspacing='2'?>
<tr bgcolor=#D0D0D0>
<th>Question</th>
<th>Strongly Disagree</th>
<th>Somewhat Disagree</th>
<th>Neither Disagree nor Agree</th>
<th>Somewhat Agree</th>
<th>Strongly Agree</th>
</tr>
<tr>
<?php
while ($row = mysql_fetch_array($res, MYSQL_ASSOC))
{
echo "<td bgcolor='#E8E8E8'><font size='-1'>$row[quest_order]) $row[quest_text]</font></td>";
for ($i = 0; $i < 5; $i++)
{
//echo "<input type='hidden' name='qID' value='$quest_id'>";
echo "<td bgcolor='#E8E8E8'><input type='radio' name='$row[quest_id]' value='$i'></td>";
}
echo "</tr>";
}
?>
<br>
</table><br>
<input type="submit" name="submitAnswers" value="Submit Survey"></fieldset>
</form>
而這裏的 'submitAnswers.php' 文件中的PHP:
$survey_id=$_POST['survey_id'];
$sql = "INSERT INTO survey_responses (survey_id)
VALUES ('$survey_id')";
$res = send_sql($sql, $link, $db);
foreach($_POST['quest_id'] as $id=>$value)
{
$sql = "INSERT INTO survey_answers (response_id, quest_id, response_value)
VALUES (LAST_INSERT_ID(), $id, '$value')";
$res = send_sql($sql, $link, $db) or die("Cannot add survey");
}
我的INSERT語句到 'survey_responses' 表工程只是很好,但我不知道如何糾正我的單選按鈕,以獲得每個問題的答案分數。
陣列([survey_id] => 4 [6] => 0 [5] => 1 [4:的值正在與POST傳遞(I通過看
print_r($_POST);
,其中返回,例如確認這] => 2 [3] => 3 [2] => 4 [1] => 3 [submitAnswers] =>提交檢驗)
但顯然我要去了解這個錯誤的,因爲I」我不能成功迭代並插入問題id#和每個響應值我相信這是相對簡單的,但我仍然是新手編程,並已經幾個小時運行幾乎沒有運氣。
只希望有人能幫我解決這個問題,並希望在這個過程中更好地解釋,或者指向一個很好的資源,涵蓋如何處理動態大小的表單,這些表單正在檢索數據庫記錄並將各種信息傳遞迴數據庫。
謝謝!
我覺得你在$ _ POST [ 'quest_id']啥都沒 –
的quest_id值是[6],[5],[4] ,在上面的數組中。 quest_id = 6,響應值(通過單選按鈕選擇)= 0.我不知道如何隔離這6個問題和響應,所以我可以將值插入到我的表中。 $ _POST發送它們,但我只是認爲我正在處理它們不正確... – PSUlion01
請清理您的數據庫輸入以及您不會被黑客攻擊:) http://roshanbh.com.np/2007/12/SQL注入攻擊 - 實例 - 和 - 防治功能於PHP。html – span