两次或多次保存后在线考试成绩重复

Online exam scores duplicating after two or more saves

我有一个具有在线考试功能的学校网络应用程序。学生可以参加考试的次数。

但是,当学生尝试考试两次或更多次并保存时,分数会在数据库中复制而不是更新。

我希望当学生再次尝试考试时,以前的分数会被覆盖而不是重复。

控制器:

 public function save()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $total_rows = $this->input->post('total_rows');
            if (!empty($total_rows)) {
                $save_result = array();
                foreach ($total_rows as $row_key => $row_value) {

                    if (isset($_POST['radio' . $row_value])) {
                        $save_result[] = array(
                            'onlineexam_student_id'  => $this->input->post('onlineexam_student_id'),
                            'onlineexam_question_id' => $this->input->post('question_id_' . $row_value),
                            'select_option'          => $_POST['radio' . $row_value],
                        );
                    }
                }
                $this->onlineexamresult_model->add($save_result);
                redirect('user/onlineexam', 'refresh');
            }
        } else {

        }
    }

型号:

 public function add($data_insert)
    {

        $this->db->trans_begin();

        if (!empty($data_insert)) {

            $this->db->insert_batch('onlineexam_student_results', $data_insert);
        }

        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

查看:

<div class="exambgtop">
                        <h3><?php echo $exam->exam; ?></h3>
                        <div class="exambgright">
                            <div id="box_header" class="inlineblock"></div>
                            <button type="button" class="btn btn-info btn-sm save_exam_btn"><?php echo $this->lang->line('submit') ?> </button>
                        </div>

试一试...

控制器:

public function save()
{
    $total_rows = $this->input->post('total_rows');
    if (empty($total_rows)) {
        redirect('user/onlineexam');
    }

    foreach ($total_rows as $row_value) {
        if ($this->input->post('radio' . $row_value) === null) {
            continue;
        }
        
        $this->onlineexamresult_model->answer(
            (int) $this->input->post('onlineexam_student_id'),
            (int) $this->input->post('question_id_' . $row_value),
            $this->input->post('radio' . $row_value)
        );
    }
    
    redirect('user/onlineexam');
}

型号:

public function answer(int $student_id, int $question_id, string $answer)
{
    // Check if the student has previously answered the question
    $current_answer = $this->get_answer_by_student_id($student_id, $question_id);
    
    if (empty($current_answer)) {
        return $this->db->insert('onlineexam_student_results', [
            'onlineexam_student_id' => $student_id,
            'onlineexam_question_id' => $question_id,
            'select_option' => $answer,
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }
    
    return $this->db->update('onlineexam_student_results', [
        'select_option' => $answer,
        'updated_at' => date('Y-m-d H:i:s')
    ], [
        'onlineexam_student_id' => $student_id,
        'onlineexam_question_id' => $question_id
    ]);
}

public function get_answer_by_student_id(int $student_id, int $question_id)
{
    return $this->db->get_where('onlineexam_student_results', [
        'onlineexam_student_id' => $student_id,
        'onlineexam_question_id' => $question_id
    ])->row_array();
}