2016-12-10 6 views
0

Ich bin neu bei PHP und als Test habe ich eine Testanwendung erstellt, die die in einer Datenbank gespeicherten Fragen lädt und sie in einer Tabelle mit Eingabetyp-Optionsfeldern anzeigt, damit der Benutzer die Antwort auswählen kann . Es gibt vier Tabellen. Fragen, Antworten, Benutzer, Userexam. Die Zeilen in jeder Tabelle enthalten eine ID als Index. Worauf ich Probleme habe, ist die Eingabe der Werte in die Datenbank, wenn auf die Schaltfläche zum Senden geklickt wird. Wie würde ich jeden Fragenwert in der Datenbank durchlaufen und hinzufügen, ohne jede ID einzeln zu definieren?Feldwerte zur Datenbank hinzufügen

<?php 

$getQuestions = " 
    SELECT 
     * 

    FROM 
     questions 
    WHERE questions.active = '1' 

    ORDER BY 
     questionID 
"; 
$qResult = mysql_query($getQuestions); 

$tableString = "<table> 
        <tr> 
         <th> 
          Questions 
         </th> 
         <th> 
          Answers 
         </th> 
        </tr>"; 

while ($qRow = mysql_fetch_assoc($qResult)) { 
    $getAnswers = " 
     SELECT 
      * 
     FROM 
      answers a 
     WHERE 
      a.questionID = '" . $qRow['questionID'] . "' 
     ORDER BY 
      a.questionID, 
      a.answerNumber 
    "; 
    $aResult = mysql_query($getAnswers); 

    $tableString .= "<tr> 
         <td>" . 
          $qRow['question'] . 
         "</td> 
         <td>"; 

    while ($aRow = mysql_fetch_assoc($aResult)) { 
     if ($aRow['correct'] == 1) { 
      $tableString .= "<input name=". $qRow['questionID'] ." 
           type='radio' 
          >" . 
           $aRow['answerValue'] . "<br />"; 
     } else { 

      $tableString .= "<input name=". $qRow['questionID'] ." 
           type='radio' 
          >" . 
           $aRow['answerValue'] . "<br />"; 
     } 
     $answer=$_POST['. $aRow["answerID"] .']; 
     $question=$_POST['. $qRow["questionID"] .']; 
     $student=$_POST['userID']; 

     // Insert data into mysql 
     $sql="INSERT INTO $userexam(answerID, questionID, userID)VALUES('$answer', '$question', '$student')"; 
     $result=mysql_query($sql); 
    } 


} 
$tableString .= "</table>"; 
echo $tableString; 
?> 
+0

ist das korrekt? $ sql = "INSERT IN $ userexam (answerID, questionID, userID) WERTE ('$ answer', '$ question', '$ student')"; –

Antwort

0

Verwenden Sie mysqli (mysql-improved) an jeder Stelle von mysql.

<?php 
//Set database conection 
$conection=mysqli_connect('localhost','root','','Your database name'); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$getQuestions = " 
    SELECT 
     * 

    FROM 
     questions 
    WHERE questions.active = '1' 

    ORDER BY 
     questionID 
"; 
$qResult = mysqli_query($conection,$getQuestions); 

$tableString = "<table> 
        <tr> 
         <th> 
          Questions 
         </th> 
         <th> 
          Answers 
         </th> 
        </tr>"; 

while ($qRow = mysqli_fetch_assoc($qResult)) { 
    $getAnswers = " 
     SELECT 
      * 
     FROM 
      answers a 
     WHERE 
      a.questionID = '" . $qRow['questionID'] . "' 
     ORDER BY 
      a.questionID, 
      a.answerNumber 
    "; 
    $aResult = mysqli_query($conection,$getAnswers); 

    $tableString .= "<tr> 
         <td>" . 
          $qRow['question'] . 
         "</td> 
         <td>"; 

    while ($aRow = mysqli_fetch_assoc($aResult)) { 
     if ($aRow['correct'] == 1) { 
      $tableString .= "<input name=". $qRow['questionID'] ." 
           type='radio' 
          >" . 
           $aRow['answerValue'] . "<br />"; 
     } else { 

      $tableString .= "<input name=". $qRow['questionID'] ." 
           type='radio' 
          >" . 
           $aRow['answerValue'] . "<br />"; 
     } 
     $answer=$_POST['. $aRow["answerID"] .']; 
     $question=$_POST['. $qRow["questionID"] .']; 
     $student=$_POST['userID']; 

     // Insert data into mysql 
     $sql="INSERT INTO userexam(answerID, questionID, userID)VALUES('$answer', '$question', '$student')"; 
     $result=mysqli_query($conection,$sql); 
    } 


} 
$tableString .= "</table>"; 
echo $tableString; 
mysqli_close($conection); 
?> 
Verwandte Themen