2014-06-13 86 views
0

已更新我的帖子6/15/14我的新問題是我有每個文本輸入旁邊的複選框,這將指示(如果選中)文本框中的文本是正確的答案。但是,當我對某個特定答案的複選框進行更改時,它未被正確保存。從數組中提取變量並插入到mysql

這裏是我的全部更新的代碼

<?php 
          //if save was clicked 
          if(isset($_POST['saveit'])) 
           { 
           $tmodnameid = $_POST['tmodnameid']; 
           $tmodnameid = mysqli_real_escape_string($dbc, $tmodnameid); 
           $quizpercent = $_POST['quizpercent']; 
           $quizpercent = mysqli_real_escape_string($dbc, $quizpercent); 

           mysqli_query($dbc, "UPDATE training_quizs set tmoduleid='$tmodnameid', quiz_passingpercent='$quizpercent' 
               WHERE quizid='$quid'"); 

           //lets delete all questions and answers since we are re-saving them 
           $q = mysqli_query($dbc, "SELECT qarowid FROM training_quizs_questions WHERE quizid='$quid'"); 
           while($qqr = mysqli_fetch_array($q)) 
           { 
            $qarowid = $qqr['qarowid']; 
            mysqli_query($dbc, "DELETE from training_quizs_answers WHERE qarowid='$qarowid'"); 
           } 
           mysqli_query($dbc, "DELETE from training_quizs_questions WHERE quizid='$quid'"); 


           $n = 0; 
           $newquestion = $_POST['newquestion']; 
            foreach($newquestion as $aquestion) 
              { 

              mysqli_query($dbc, "INSERT into training_quizs_questions(quizid, qquestion) 
                  VALUES('$quid','$aquestion')"); 
              $questionrowid = mysqli_insert_id($dbc); 

              //lets save each of this questions answers 

              //save first potential answer for this question 
               $answera = $_POST['answera'][$n]; 
               $isanswera =''; 
               $isanswera = trim($_POST['isanswera'][$n]); 
               if(!empty($isanswera)) 
               { 
               $isanswera ='yes'; 
               $firstsql = mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answera','$isanswera')"); 
               }else{ 
               $isanswera = ''; 
               $firstsql = mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answera',' ')"); 
               } 



              //save second potential answer for this question 
               $answerb = $_POST['answerb'][$n]; 
               $isanswerb=''; 
               $isanswerb = trim($_POST['isanswerb'][$n]); 
               if(!empty($isanswerb)) 
               { 
               $isanswerb ='yes'; 
                mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerb','$isanswerb')"); 
               }else{ 
               $isanswerb = ''; 
                mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerb',' ')"); 
               } 


              //save third potential answer for this question 
               $answerc = $_POST['answerc'][$n]; 
               $isanswerc = trim($_POST['isanswerc'][$n]); 
               if(!empty($isanswerc)) 
               { 
               $isanswerc ='yes'; 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerc','$isanswerc')"); 
               }else{ 
               $isanswerc = ''; 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerc',' ')"); 
               } 


              //save fourth potential answer for this question 
               $answerd = $_POST['answerd'][$n]; 
               $isanswerd = trim($_POST['isanswerd'][$n]); 
               if(!empty($isanswerd)) 
               { 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerd','yes')"); 
               }else{ 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerd',' ')"); 
               } 
              //save fifth potential answer for this question 
               $answere = $_POST['answere'][$n]; 
               $isanswere = trim($_POST['isanswere'][$n]); 
               if(!empty($isanswere)) 
               { 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answere','yes')"); 
               }else{ 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answere',' ')"); 
               } 
              //save sixth potential answer for this question 
               $answerf = $_POST['answerf'][$n]; 
               $isanswerf = trim($_POST['isanswerf'][$n]); 
               if(!empty($isanswerf)) 
               { 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerf','yes')"); 
               }else{ 
               mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) 
                  VALUES('$questionrowid','$answerf',' ')"); 
               } 
               $n++; 
              } 
              //get rid of any blank rows that were created 
               mysqli_query($dbc, "DELETE from training_quizs_answers WHERE answer=''"); 

           echo "<div class='alert alert-success'>     
              <h4>Success!</h4> 
              You successfully setup a new quiz from the training module. 
             </div>"; 

           } 

           //lets get the questions 
            $qsql = mysqli_query($dbc, "SELECT * FROM training_quizs WHERE quizid='$quid' LIMIT 1"); 
            $qsqlrow = mysqli_fetch_array($qsql); 
            extract($qsqlrow); 
            $quiz_createdon = date('m/d/Y', strtotime($quiz_createdon)); 
         ?> 
         <form method='POST' action='' enctype='multipart/form-data' name='form1' id='form1'> 
         <div class="row-form clearfix"> 
          <div class="span4"> 
           Training Module Name 
           <select name='tmodnameid'> 
           <?php 
            //lets select all training modules that have not quiz yet 
             $atlistsql = mysqli_query($dbc, "SELECT tmoduleid, tmodule_name FROM training_modules WHERE 
                   tmoduleid='$tmoduleid' LIMIT 1"); 
             $atlistrow = mysqli_fetch_array($atlistsql); 
             $tmoduleid = $atlistrow['tmoduleid']; 
             $tmodule_name = $atlistrow['tmodule_name']; 
             echo "<option value='$tmoduleid'>$tmodule_name</option>"; 

             $tlistsql = mysqli_query($dbc, "SELECT tmoduleid, tmodule_name FROM training_modules WHERE tmoduleid !='$tmoduleid' 
                   ORDER by tmodule_name desc"); 
             while($tlistrow = mysqli_fetch_array($tlistsql)) 
             { 
              $tmoduleid = $tlistrow['tmoduleid']; 
              $tmodule_name = stripslashes($tlistrow['tmodule_name']); 
              echo "<option value='$tmoduleid'>$tmodule_name</option>"; 
             } 
           ?> 
           </select> 
          </div> 
          <div class="span2"> 
           Percent needing to pass<br /> 
           <input type='text' name='quizpercent' style='width:50px' maxlength='3' value='<?php echo "$quiz_passingpercent";?>'></strong>%</strong> 
          </div> 
         </div> 

         <div class="row-form clearfix"> 
          <?php 
           $question_num = 1; 
           //lets list all the questions 
            $qsql = mysqli_query($dbc, "SELECT * FROM training_quizs_questions WHERE quizid='$quid' ORDER BY qarowid asc"); 
            while($qrow = mysqli_fetch_array($qsql)) 
            { 
            extract($qrow); 
            $qquestion = stripslashes($qquestion); 
            echo "<strong>$question_num)</strong><br /><textarea name='newquestion[]'>$qquestion</textarea><br /> 
              <br /><strong>Question $question_num Potential Answers</strong><br />"; 

            //lets get all the potential answers 
             $numpotentialanswers = 0; 
             $letter = 'a'; 
             $displayletter = 'A'; 
             $qaaql = mysqli_query($dbc, "SELECT * FROM training_quizs_answers WHERE qarowid='$qarowid' 
                  ORDER by answerrow ASC"); 
               while($qarow = mysqli_fetch_array($qaaql)) 
                { 
                extract($qarow); 
                $answer = stripslashes($answer); 
                $numpotentialanswers++; 

                if($isanswer =='yes') 
                 { 
                 $checked ='checked'; 
                 } 
                 else 
                 { 
                 $checked =''; 
                 } 
          $answerletter[] = "answer$letter"; 
          $isanswerletter[] = "isanswer$letter"; 

              ?> 
               <div style='float:left; padding-right:4px'> <?php echo "$displayletter";?>) 
                <input type='text' <?php echo "name='answer" . $letter. "[]'"; ?> value='<?php echo "$answer";?>' style='width:400px'><br /><br /> 
                  </div> 
                  <div style='float:left'> 
                   is answer <input type='checkbox' name='isanswer<?php echo "$letter";?>[]' value='yes' <?php echo "$checked";?>> 
                  </div><div style='clear:both'></div> 
              <?php 
                $letter++; 
                $displayletter++; 
                } 
              //lets display the additional potential answer rows 
              if($numpotentialanswers == 1) 
              { 
               echo "<div style='float:left; padding-right:4px'> B) 
                  <input type='text' name='answerb[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerb[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> C) 
                  <input type='text' name='answerc[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerc[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> D) 
                  <input type='text' name='answerd[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerd[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> E) 
                  <input type='text' name='answere[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswere[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> F) 
                  <input type='text' name='answerf[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerf[]' value='yes'> 
                 </div><div style='clear:both'></div>"; 
              } 
              if($numpotentialanswers == 2) 
              { 
               echo "<div style='float:left; padding-right:4px'> C) 
                  <input type='text' name='answerc[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerc[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> D) 
                  <input type='text' name='answerd[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerd[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> E) 
                  <input type='text' name='answere[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswere[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> F) 
                  <input type='text' name='answerf[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerf[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 "; 
              } 
              if($numpotentialanswers == 3) 
              { 
               echo "<div style='float:left; padding-right:4px'> D) 
                  <input type='text' name='answerd[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerd[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> E) 
                  <input type='text' name='answere[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswere[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> F) 
                  <input type='text' name='answerf[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerf[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 "; 
              } 
              if($numpotentialanswers == 4) 
              { 
               echo "<div style='float:left; padding-right:4px'> E) 
                  <input type='text' name='answere[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswere[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 <div style='float:left; padding-right:4px'> F) 
                  <input type='text' name='answerf[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerf[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 "; 
              } 
              if($numpotentialanswers == 5) 
              { 
               echo "<div style='float:left; padding-right:4px'> F) 
                  <input type='text' name='answerf[]' style='width:400px'><br /><br /> 
                 </div> 
                 <div style='float:left'> 
                  is answer <input type='checkbox' name='isanswerf[]' value='yes'> 
                 </div><div style='clear:both'></div> 
                 "; 
              } 
             $question_num++; 
            } 

          ?> 
          <br /> 
          <input type="button" onclick="addInput()" name="add" value="Add Question" /><br /> 
          <div id='text'></div>       
         </div> 


         <div class="footer tar"> 
          <input type='submit' name='saveit' value='Save' is='submit' class='btn'></form> 
         </div>        
        </div> 
+0

詢問'mysqli_error()'查詢是否成功。 – mario

+0

mysqli_error()不會顯示任何錯誤,並且每個文本框的第一個字符都被插入到數據庫中,因此我不希望返回錯誤。我認爲我的錯誤與php本身有關,儘管我無法發現它。 – Jayreis

+1

你的問題不包含任何有關實際表單或'$ _POST'結構的信息。另外'error_reporting'。 – mario

回答

0

無表單字段具有陣列names[],所以訪問$_POST['answera'][$n]只會提取一個字母。

+0

謝謝馬里奧。我做了一個改變,但現在得到一個分析錯誤:語法錯誤,意外的']',期望標識符(T_STRING)或變量(T_VARIABLE)或數字(T_NUM_STRING),這是我有 Jayreis

+0

好的我想通了我的問題是我混合HTML和PHP。所以我解決了一個問題,但我的另一個問題在每個文本輸入框旁邊。我有一個複選框,如果選中該複選框,則值爲yes,表示寫入文本框中的文本是正確的答案。 我的問題是,複選框沒有與正確的關聯文本框條目一起保存。請參閱我的OP獲取更新的代碼 – Jayreis