2014-04-08 71 views
0

我試圖獲得調查的回覆,所有問題的答案都顯示在while循環中並且在動作中PHP頁面我沒有得到正確的響應,我得到了正確的響應對於單選按鈕,我附上了代碼。在foreach循環中獲取多個表單輸入值

<?php 
    $i = 1; 
    while ($row = mysqli_fetch_array($questions)) { 
?> 
<div class="control-group"> 
    <label class="control-label" for="focusedInput">(<?php echo $i; ?>) 
    <?php 
     $questionid = $row['question_id']; 
     $question = $row['question']; 
    ?> 
    <input type="hidden" name="questionid" value="<?php echo $questionid; ?>" /> 
    <input type="hidden" name="question" value="<?php echo $question; ?>" /> 
    <?php echo $row['question']; ?></label> 
    <div class="controls"> 
    <?php 
     if ($row['answer_type'] == "Ratings") { 
      echo "<p> 
       Low<input type='radio' name='rating$i' value='1' id='rating_0'>                           
       <input type='radio' name='rating$i' value='2' id='rating_1'>               
       <input type='radio' name='rating$i' value='3' id='rating_2'>               
       <input type='radio' name='rating$i' value='4' id='rating_3'>              
       <input type='radio' name='rating$i' value='5' id='rating_4'>High             
      </p>"; 
     } else if ($row['answer_type'] == "Comments") { 
      echo "<textarea name='answer' cols='' rows=''></textarea>"; 
     } 
     $i++; 
     echo "<br />"; 
    ?> 
    </div> 
</div> 
<?php } ?> 

行動文件代碼:

foreach($_POST as $val){ 
    $query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$_SESSION[surveyid],$_POST[questionid],'$_POST[question]',$val,'$_POST[answer]')"; 

    $result2 = mysqli_query($con,$query2);              

    if(!$result2) { 
     echo mysqli_error($result2); 
    } 
} 

enter image description here

我想插入在MySQL表包括在輸出圖像顯示領域的調查答案。

+0

什麼結果你? – user3454436

+0

你應該使用** mysqli_stmt_prepare()**。你不需要'foreach'。 – blue

+0

結果在屏幕截圖中出現! @blue可以請你發佈相同的代碼片段! –

回答

0

您的form標籤缺少暴露的代碼,所以可能會有一些額外的字段可用,這對場景很重要。

$_POST上沒有明顯的理由使用foreach。你應該進一步解釋你爲什麼要騎自行車。

這裏是爲您的操作文件的一些代碼,這可能會爲你工作:

/* create a prepared statement */ 
$stmt = $mysqli->stmt_init(); 
if ($stmt->prepare("INSERT INTO review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) VALUES(1,?,?,?,?,?)")) { 

    /* bind parameters for markers */ 
    $stmt->bind_param("sssss", $_SESSION['surveyid'], $_POST['questionid'], $_POST['question'], $val, $_POST['answer']); 

    /* execute query */ 
    $stmt->execute(); 

    /* close statement */ 
    $stmt->close(); 
} 
+0

我想代碼中缺少的東西,我不知道確切的,但我試過這個,不起作用。 –