Php 在线考试分数在两次或多次保存后复制

Php 在线考试分数在两次或多次保存后复制,php,codeigniter,Php,Codeigniter,我有一个具有在线考试功能的学校网络应用程序。学生可以尝试多次考试 但是,当学生尝试考试两次或两次以上并保存时,分数将在数据库中复制,而不是更新 我希望当学生再次尝试考试时,之前的分数会被覆盖而不是重复 控制器: public function save() { if ($this->input->server('REQUEST_METHOD') == 'POST') { $total_rows = $this->input-&

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

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

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

控制器:

 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 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 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;
        }
    }
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();
}
视图:


试试看

控制器:

 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 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 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;
        }
    }
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();
}

请向我们展示您的视图部件。看起来您希望“批量替换为”。。。(不需要显示视图文件…实际上,
'question\u id.$row\u value
表示您没有正确地使用数组语法声明字段
name
s)@KUMAR我已经包含了视图文件“Try this”(试一试)答案在堆栈溢出上的价值很低,因为它们在教育/授权OP和数千名未来研究人员方面做得很差。