[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 ?