codeigniter 防止插入到数据库如果合计值等于0,

7uzetpgm  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(131)

当为学生添加分数时,我希望那些总分为0的分数根本不插入数据库。
控制器:

public function entrymarks()
 {
 $this->form_validation->set_error_delimiters('', '');
 $this->form_validation->set_rules('exam_group_class_batch_exam_subject_id', 'Subject', 'required|trim|xss_clean');

 if ($this->form_validation->run() == false) {
 $data = array(
 'exam_group_class_batch_exam_subject_id' => form_error('exam_group_class_batch_exam_subject_id'),
 );
 $array = array('status' => 0, 'error' => $data);
 echo json_encode($array);
 } else {

 $exam_group_student_id = $this->input->post('exam_group_student_id');

 $insert_array = array();
 $update_array = array();
 if (!empty($exam_group_student_id)) {
 foreach ($exam_group_student_id as $exam_group_student_key => $exam_group_student_value) {
 $attendance_post = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
 if (isset($attendance_post)) {
 $attendance = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
 } else {
 $attendance = "present";
 }

 $array = array(
 'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
 'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
 'attendence' => $attendance,
 'get_ca1' => $this->input->post('exam_group_student_ca1_' . $exam_group_student_value),
 'get_ca2' => $this->input->post('exam_group_student_ca2_' . $exam_group_student_value),
 'get_ca3' => $this->input->post('exam_group_student_ca3_' . $exam_group_student_value),
 'get_ca4' => $this->input->post('exam_group_student_ca4_' . $exam_group_student_value),
 'get_exam' => $this->input->post('exam_group_student_exam_' . $exam_group_student_value),
 'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value),
 
 );
 $insert_array[] = $array;
 }
 }

 if ( intval($array['get_ca1'] +$array['get_ca2']+$array['get_ca3']+$array['get_ca4']+$array['get_exam'] ) > 0 ) {
 $this->examgroupstudent_model->add_result($insert_array);
 }

 }
 $array = array('status' => '1', 'error' => '', 'message' => $this->lang->line('success_message'));
 echo json_encode($array);
 }
 }

我想先得到get_ca1 + get_ca2 + get_ca3 + get_ca4 + get_exam的总和
那么如果它是0,则不插入。
我该怎么做?

1cosmwyk

1cosmwyk1#

我不认为您可以在没有任何解决方法的情况下即时访问数组值,并且您已经将insert放在了foreach之外。
你最好多花点时间在这些上面。
1.代码索引
1.使用正确的IDE。不像记事本。
1.调试所有和每一行的错误。
这样做。
由于您没有使用批量插入,因此可以逐个插入。

$cal1 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal2 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal3 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal4 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$exam = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);

$total = $cal1 + $cal2 + $cal3 + $cal4 + $exam;

$array = array(
    'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
    'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
    'attendence' => $attendance,
    'get_ca1' => $cal1,
    'get_ca2' => $cal2,
    'get_ca3' => $cal3,
    'get_ca4' => $cal4,
    'get_exam' => $exam,
    'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value)
);

if ($total > 0) {
    $this->examgroupstudent_model->add_result($array);
}

最终代码是

public function entrymarks()
{
    $this->form_validation->set_error_delimiters('', '');
    $this->form_validation->set_rules('exam_group_class_batch_exam_subject_id', 'Subject', 'required|trim|xss_clean');

    if ($this->form_validation->run() == false) {
        $data = array(
            'exam_group_class_batch_exam_subject_id' => form_error('exam_group_class_batch_exam_subject_id'),
        );
        $array = array('status' => 0, 'error' => $data);
        echo json_encode($array);
    } else {

        $exam_group_student_id = $this->input->post('exam_group_student_id');

        $insert_array = array();
        $update_array = array();
        if (!empty($exam_group_student_id)) {
            foreach ($exam_group_student_id as $exam_group_student_key => $exam_group_student_value) {
                $attendance_post = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
                if (isset($attendance_post)) {
                    $attendance = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
                } else {
                    $attendance = "present";
                }

                $cal1 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
                $cal2 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
                $cal3 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
                $cal4 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
                $exam = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);

                $total = $cal1 + $cal2 + $cal3 + $cal4 + $exam;

                $array = array(
                    'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
                    'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
                    'attendence' => $attendance,
                    'get_ca1' => $cal1,
                    'get_ca2' => $cal2,
                    'get_ca3' => $cal3,
                    'get_ca4' => $cal4,
                    'get_exam' => $exam,
                    'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value)
                );

                if ($total > 0) {
                    $this->examgroupstudent_model->add_result($array);
                }
            }
        }
    }

    $array = array('status' => '1', 'error' => '', 'message' => $this->lang->line('success_message'));
    echo json_encode($array);
}

相关问题