2013-10-23 60 views
-2

請參考下面的圖片:PHP - 如何選擇由mysqli_fetch_array()給出的行的特定索引?

http://i.stack.imgur.com/6hBPC.png

例如,如果用戶點擊該行的按鈕,說:「你有數學測驗」時,認爲「問答ID」值行然後會被傳遞給另一個PHP文件。

這裏是我當前的代碼:

<?php 
    $con=mysqli_connect("127.0.0.1", "root", "", "quizmaker"); 
    if (mysqli_connect_errno($con)) 
    { 
     echo "MySqli Error: " . mysqli_connect_error(); 
     } 

    $now=date("m/d/Y"); 
    $sql=mysqli_query($con,"SELECT * FROM quiz_query WHERE quiz_date='$now'"); 
    $count=mysqli_num_rows($sql); 

    if($count>=1) 
    { 
    echo "<table border='1' width='50%'>"; 
    echo "<form action='answer_quiz.php' method='post'>"; 
    echo "<tr> 
    <td>You have a pending quiz!</td><td>&nbsp;</td><td>&nbsp;</td> 
    </tr>"; 
    $number=1; 

    while($result=mysqli_fetch_array($sql)) 
    { 
     echo "<tr>"; 
     echo "<td>You have a quiz for " . $result['subject'] . "</td>"; 
     echo "<td>Quiz ID: " .$result['quiz_ID']. "</td>"; 
     echo "<td><input type='submit' name='button' id='button' value='Take Quiz'>"; 
     echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>"; 
     echo "</td>"; 
     echo "</tr>"; 
     $number++; 
     } 


    echo "</form>"; 
    echo "</table>";  
    } 
    else 
    { 
     "You have no quiz! :D"; 
     } 

    mysqli_close($con); 
    ?> 
+0

歡迎SO!你能提供更多關於你在找什麼的信息嗎?你是否遇到錯誤?您沒有列出上述代碼的明確問題。 – Derek

+0

謝謝您的關注,但問題已解決。 :) –

回答

1

移動這一行:

echo "<form action='answer_quiz.php' method='post'>"; 

裏面的while循環。


而且,變化

echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>" 

echo "<input type='hidden' name='quizId' value='$result[quiz_ID]'>" 

現在,在answer_quiz.php您會收到$_POST['quizId']與您需要的值。

+0

Damnit,Saturnix。你的字面速度比我快5秒:D –

+0

我知道這感覺很棒! :( – Saturnix

+0

無論如何都有這個+1 :) –

0

變化的,同時:

while($row = $result->fetch_array(MYSQLI_ASSOC)){ 

echo $row['subject']; 

} 
0

你忘了在你的變量報價: 相反

echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>"; 

應該

echo "<input type='hidden' name='quiz[$number]' value='$result[\"quiz_ID\"]'>"; 
相關問題