我有一個php questionare頁面,它會從數據庫中生成多個問題。所有的問題都是錯誤的,但有幾個問題還有一個補充文本字段(見圖片)。如果填充文本字段('fill_in_quest_text')中包含文本,則將響應和文本保存到msyslq數據庫中。但是,如果問題的文本框中沒有文本,我不希望提交它隨附的問題編號的回覆。我認爲有一個簡單的解決方案,但是我對PHP的知識還不夠豐富,無法知道如何去做 - 即只在文本框中有文本時纔將響應插入到數據庫中。希望空的textfield問題不能被輸入到數據庫中
這裏是什麼問題,並填寫問題是這樣的:
下面是相關的代碼從_POST變量取的響應,並把它們納入「響應」表。
foreach($_POST['question'] as $key => $questionToInsert)
{
$follow = (isset($_POST['follow'][$key])) ? $_POST['follow'][$key] : '';
$fill_in_quest_text = (isset($_POST['fill_in_quest_text'][$key])) ?
$_POST['fill_in_quest_text'][$key] : '';
$q = (isset($_POST['q'][$key])) ? $_POST['q'][$key] : '';
$query = "insert into responses(userid,question_id,response_main,
response_followup,category,response_time,unique_session,fill_in_quest_text)
values('".$_SESSION['userid']."','".$_POST['question'][$key]."', '".$q."',
'".$follow."','".$_POST["category"]."','".date("Y-m-d")."',
'".$_SESSION['unique_session']."','".$fill_in_quest_text."')";
mysql_query($query);
}
以下是瀏覽器中的HTML代碼,與上面的截圖相對應。 (對不起,亂七八糟)
<tr>
<td width="70" valign="top"><input type="radio" name="q[37]" value="yes" onClick='callme("q37","y","follow37")'> Yes
<input type="hidden" name="question[37]" value="364"></td>
<td width="70" valign="top"><input type="radio" name="q[37]" value="no" onClick='callme("q37","n","follow37")' >
No</td>
<td>I neither regret the past nor wish to close the door on it.</td>
</tr>
<tr>
<td width="70" valign="top"> </td>
<td width="70" valign="top"> </td>
<td ><div id="q37" style="display:none">
<input type="checkbox" id="follow37" name="follow[37]" value="yes" >
I would like to work on this</div>
<br>
</td></tr>
<tr>
<td width="70" valign="top"><input type="radio" name="q[38]" value="yes" onClick='callme("q38","y","follow38")'>
Yes
<input type="hidden" name="question[38]" value="365"></td>
<td width="70" valign="top"><input type="radio" name="q[38]" value="no" onClick='callme("q38","n","follow38")' >
No</td>
<td>I read meaningful texts and discuss them with others.</td>
</tr>
<tr>
<td width="70" valign="top"> </td>
<td width="70" valign="top"> </td>
<td ><div id="q38" style="display:none">
<input type="checkbox" id="follow38" name="follow[38]" value="yes" >
I would like to work on this</div>
<br>
</td></tr>
<tr>
<td width="70" valign="top"><input type="radio" name="q[39]" value="yes" onClick='callme("q39","y","follow39")'>
Yes
<input type="hidden" name="question[39]" value="366"></td>
<td width="70" valign="top"><input type="radio" name="q[39]" value="no" onClick='callme("q39","n","follow39")' >
No</td>
<td>Other: <input style='font-size=13px;' type='text' size='90' name='fill_in_quest_text[39]'></td>
</tr>
<tr>
<td width="70" valign="top"> </td>
<td width="70" valign="top"> </td>
<td ><div id="q39" style="display:none">
<input type="checkbox" id="follow39" name="follow[39]" value="yes" >
I would like to work on this</div>
<br>
</td></tr>
<tr>
<td width="70" valign="top"><input type="radio" name="q[40]" value="yes" onClick='callme("q40","y","follow40")'>
Yes
<input type="hidden" name="question[40]" value="367"></td>
<td width="70" valign="top"><input type="radio" name="q[40]" value="no" onClick='callme("q40","n","follow40")' >
No</td>
<td>Other: <input style='font-size=13px;' type='text' size='90' name='fill_in_quest_text[40]'></td>
</tr>
<tr>
<td width="70" valign="top"> </td>
<td width="70" valign="top"> </td>
<td ><div id="q40" style="display:none">
<input type="checkbox" id="follow40" name="follow[40]" value="yes" >
I would like to work on this</div>
<br>
</td>
</tr>
<tr>
<td colspan="3"><input id="btns" type="submit" name="submit" value=" Submit " onClick="return countUAquestions()">
<!-- <input type="button" name="button" value="Cancel" onClick="window.location.href='welcome_page.php'"> -->
<input id="btns" type="button" name="button" value=" Cancel " onClick="return jsconfirm()"></td>
</tr>
<tr>
<td colspan="3" height="20"> </td>
</tr>
</table>
<input type="hidden" name="category" value="Spiritual_Wellness">
<input type="hidden" name="total_questions" value="40">
</form>
</div>
讓我知道如果我需要提供更多的細節。我認爲它也可以用JavaScript來完成,但也不能完全弄清楚。
謝謝。
SQL注入? ... – 2015-03-31 07:30:14