Welcome Guest, Not a member yet? Register   Sign In
Insertion in double
#1

[eluser]Unknown[/eluser]
Hi guys ... First of all, English isn't my mother language, so please don't take care about my mistakes !

I am new in CI and I have a problem with an insertion in my DB. Indeed, when I insert a recording, it works well but when I check in my table, 2 recordings have been inserted (instead of once) : the first is correct, and the 2nd is done nearly at the same time (not even a second after the first one).

Here's the code of the function which saves a score in the DB, in my controler :
Code:
function check_answer()               {
                              $gdAnswer= 0;
                              $wrgAnswer = 0;
                              $categorie_ID = $this->session->userdata("categorie_ID");

                              // parcours des diffĂ©rentes rĂ©ponses faites par l'internaute
                              for($j = 1; $j < 3; $j++)
                              {
                                             $question_ID = $this->input->post("Question_ID" .$j);
                                             $answer = $this->input->post("Question_answer" .$j);
                                             $checkedChoice = $this->input->post("choice".$j);

                                             if($checkedChoice != NULL)
                                             {
                                                            if($checkedChoice == $answer)
                                                            {
                                                                           $gdAnswer++;
                                                            }
                                                            else
                                                            {
                                                                           $wrgAnswer++;
                                                            }
                                             }
                              }
                              $score = ($gdAnswer*3) - ($wrgAnswer*2);
$this->test_model->save_test($score, $categorie_ID);                                                                            
               }

Here's the code of the function save_test() of my model 'Test_model':
Code:
public function save_test($score, $categorie_ID)
               {
                              $data = array(
                                             "Test_user_ID" => $this->session->userdata("user_ID"),
                                             "Test_categorie_ID" => $categorie_ID,
                                             "Test_note" => $score,
                                             "Test_date" => date("Y-m-d H:m:s"));
                              return $this->db->insert($this->table, $data);
               }

And the view which displays the list of questions :
Code:
La vue qui affiche le QCM :

echo form_open("test/check_answer");

$j = 0;
foreach ($query->result() as $row):
{
               $j++;
               $nameRadios = "choice" .$j;
               $nameQuestion = "Question_ID" .$j;
               $nameAnswer = "Question_answer" .$j;
              
               echo form_hidden($nameQuestion , $row->Question_ID);
               echo form_hidden($nomReponse, $row->Question_reponse);
               echo "<h2>" .$row->Question_question. "</h2>
";
               for($numAnswer = 1; $numAnswer< 5; $numAnswer++)
               {
                              $choix = "Question_choix".$numAnswer;
                              echo form_radio(array("name" => $nameRadios , "value" => $numAnswer));
                              echo form_label($row->$choice, $nameRadios );
                                        }
} endforeach;

echo form_submit("validate_questions", "Validate");
echo form_close();


Do you know why I have 2 recordings in my DB instead of once ?
#2

[eluser]toopay[/eluser]
I dont see any line on your controller, which calls the model function. Include how you save the data or calls the model function.
#3

[eluser]Unknown[/eluser]
Sorry, that was hidden by the wrong indentation of the code, I've edited my post.
#4

[eluser]toopay[/eluser]
You doesnt need to return it! If you want to see if the entries was succesfully executed, return the affected row, like
Code:
// On your model
public function save_test($score, $categorie_ID)
{
   $data = array(
                 "Test_user_ID" => $this->session->userdata("user_ID"),
                 "Test_categorie_ID" => $categorie_ID,
                 "Test_note" => $score,
                 "Test_date" => date("Y-m-d H:m:s")
           );
   $this->db->insert($this->table, $data);
   return $this->db->affected_rows();
}




Theme © iAndrew 2016 - Forum software by © MyBB