2014-10-27 52 views
0

我在數據庫中顯示了我的數據表格,左邊是複選框。我想找到一種方法將複選框鏈接到問題編號(ID)。當我點擊提交時,我想要選中的ID被回顯。幾乎我希望有人能夠選擇他們想要的問題然後顯示出來。如何獲取顯示的數據庫QUICKFIX上所有選中框的ID?

<?php 

    $con=mysqli_connect("####","####","#####","###"); 

    $result = mysqli_query($con,"SELECT * FROM q_and_a "); 

echo "<table border='1'> 
    <tr> 
<th>Add</th> 
<th>#</th> 
<th>Question</th> 
<th>A</th> 
<th>B</th> 
<th>C</th> 
<th>D</th> 
<th>Answer</th> 

</tr>"; 

while($row = mysqli_fetch_array($result)) 
{ 
echo "<tr>"; 

echo '<td><input type="checkbox" name="questions[]" value="$id"></td>'; 

echo "<td>" . $row['id'] . "</td>"; 
echo "<td>" . $row['question'] . "</td>"; 
echo "<td>" . $row['choiceA'] . "</td>"; 
echo "<td>" . $row['choiceB'] . "</td>"; 
echo "<td>" . $row['choiceC'] . "</td>"; 
echo "<td>" . $row['choiceD'] . "</td>"; 
echo "<td>" . $row['answer'] . "</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

mysqli_close($con); 

?> 

提交按鈕

<form method="POST" action="makeTest.php"> 
<input type="submit" name="make" value="Make Test"> 
</form> 
+2

只是CHEC k一些值並提交表單,通過'$ _POST ['questions']' – Ghost 2014-10-27 08:54:23

+0

獲取值'POST'輸入字段應該在'form'標記中。 – 2014-10-27 09:06:26

回答

0

趕快做些編輯您的代碼,然後它會工作。 1.改變您的查詢通過添加字段不*(以確保性能和顯示順序)

$result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a "); 
  • 然後之前而塊開放的形式標記(HTML)

    <?php 
    //above codes will be there as you show before 
    echo '<form method="POST" action="makeTest.php">'; 
    while($row = mysqli_fetch_array($result)){ 
    { $id=$row['id']; // initialize your id here, so as to pass it in checkbox too 
    // then continue with your code 
    } 
    ?> 
    <input type="submit" name="make" value="Make Test"> 
    </form> 
    
  • 在maketest.php喲可以喊得ckeckbox使用的foreach,見下文

    foreach($_POST['questions'] as $questions){ 
              //do your job 
    
        } 
    
    +0

    這工作更有效率,但它總是說,ID是4.如何ü告訴我如何打印哪些ids點擊時,我打測試。我點擊使2與選擇,它讓我回2 4的 – 2014-10-27 11:40:38

    +0

    我想我不明白你想要朋友 – 2014-11-02 20:44:03

    +0

    後for循環excecu的id卡住在4,所以它只出4s。而不是一兩三四。我想你沒有測試你的代碼,所以你顯然不知道你在做什麼。 – 2014-11-03 20:09:26