2011-06-21 102 views
-3

我想要做的是運行一個選擇語句爲每個答案選擇數據庫中的答案,其中questionID = $iuserID = $userID所以我有這樣的查詢設置到目前爲止,但不知道我的'失蹤或我是否正確,沒有失去任何東西?另外無論我做什麼兩個字段都有值,但我仍然收到我需要填寫兩個表單域的錯誤消息。表格處理

<?php 
         $i = 1; 
         while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
         ?> 
          <dl> 

           <dt style="width: 190px;"><label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?></label></dt> 
           <dd><input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" size="54" /></dd> 

          </dl> 
         <?php 
         ++$i; 
         } 
         ?> 

if (empty($_POST['answer1'][$i]) || trim($_POST['answer1'][$i])=="") {$errors = "yes";} 
if (empty($_POST['answer2'][$i]) || trim($_POST['answer2'][$i])=="") {$errors = "yes";} 

// Error checking, make sure all form fields have input 
if ($errors == "yes") { 

    // Not all fields were entered error 
    $message = "You must enter values to all of the form fields!"; 

    $output = array('errorsExist' => true, 'message' => $message); 

} else { 

    $userID = mysqli_real_escape_string($dbc,$_POST['userID']); 
    $answer1 = mysqli_real_escape_string($dbc,$_POST['answer1'][$i]); 
    $answer2 = mysqli_real_escape_string($dbc,$_POST['answer2'][$i]); 

    $query = "SELECT * FROM manager_users_secretAnswers WHERE questionID = '".$questionID."' AND userID = '".$userID."'"; 
    $result = mysqli_query($dbc,$query); 
    echo $query; 
+1

哥們_ $ {ID} ..你一直在重複同樣的即使我們告訴你要這樣做,也是錯誤的。空(修剪($ _ POST ['answer1'] [$ i]))就足夠了,你不必做額外的||事情。 – FinalForm

+1

PEAR :: QuickForm – powtac

回答

2

您可能會遇到php類型autoquessing的問題。讓假設閣下有權使用IDS問題:3,5,7,8,那麼你正在使用:

empty($_POST['answer1'][$i]) 

$ _ POST [「ANSWER1」] [3]所以你取數組的第三個元素。

因此,我建議使用不陣列的符號,而是: 對於輸入名稱:答案| $ {ID}或回答,而不是答案[$ ID]