Codeigniter数据库更新问题

时间:2016-10-12 14:35:21

标签: php sql database codeigniter

我想更新我的数据,但 mark_obtained 列不会更新。我不知道为什么,我的代码中有什么问题吗?

这是我的控制器代码:

function marks($exam_id = '', $class_id = '', $subject_exam_id = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(base_url(), 'refresh');

        if ($this->input->post('operation') == 'selection') {
            $page_data['exam_id']    = $this->input->post('exam_id');
            $page_data['class_id']   = $this->input->post('class_id');
            $page_data['subject_exam_id'] = $this->input->post('subject_exam_id');

            if ($page_data['exam_id'] > 0 && $page_data['class_id'] > 0 && $page_data['subject_exam_id'] > 0) {
                redirect(base_url() . 'index.php?admin/marks/' . $page_data['exam_id'] . '/' . $page_data['class_id'] . '/'  . $page_data['subject_exam_id'], 'refresh');
            } else {
                $this->session->set_flashdata('mark_message', 'Choose exam, class and subject');
                redirect(base_url() . 'index.php?admin/marks/', 'refresh');
            }
        }
        if ($this->input->post('operation') == 'update') {
            $students = $this->db->get_where('enroll' , array('class_id' => $class_id , 'year' => $running_year))->result_array();
            foreach($students as $row) {
                $data['mark_obtained'] = $this->input->post('mark_obtained_' . $row['student_id']);
                $data['comment']       = $this->input->post('comment_' . $row['student_id']);

                $this->db->where('mark_id', $this->input->post('mark_id_' . $row['student_id']));
                $this->db->update('mark', array('mark_obtained' => $data['mark_obtained'] , 'comment' => $data['comment']));
            }
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(base_url() . 'index.php?admin/marks/' . $this->input->post('exam_id') . '/' . $this->input->post('class_id') . '/' . $this->input->post('subject_exam_id'), 'refresh');
        }
        $page_data['exam_id']    = $exam_id;
        $page_data['class_id']   = $class_id;
        $page_data['subject_exam_id'] = $subject_exam_id;

        $page_data['page_info'] = 'Exam marks';

        $page_data['page_name']  = 'marks';
        $page_data['page_title'] = get_phrase('manage_exam_marks');
        $this->load->view('backend/index', $page_data);
    }

这是我的观点:

 <?php if($exam_id >0 && $class_id >0 && $subject_exam_id >0 ):?>
                <?php 
                        ////CREATE THE MARK ENTRY ONLY IF NOT EXISTS////
                        $students   =   $this->db->get_where('enroll' , array(
                            'year' => $running_year , 'class_id' => $class_id
                        ))->result_array();
                        foreach($students as $row):
                            $verify_data    =   array(  'exam_id'           => $exam_id ,
                                                        'class_id'          => $class_id ,
                                                        'subject_exam_id'   => $subject_exam_id ,
                                                        'year'              => $running_year, 
                                                        'student_id'        => $row['student_id']);
                            $query = $this->db->get_where('mark' , $verify_data);

                            if($query->num_rows() < 1)
                                $this->db->insert('mark' , $verify_data);
                         endforeach;


<input type="number" value="<?php echo $row2['mark_obtained'];?>" name="mark_obtained" class="form-control" >

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

if ($this->input->post('operation') == 'update') {
    $data['mark_obtained'] = $this->input->post('mark_obtained');
    $data['comment']       = $this->input->post('comment');

    $this->db->where('mark_id', $this->input->post('mark_id'));
    $this->db->update('mark_obtained', $data);  // ***MAYBE THIS WAS THE ISSUE****
    $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
    redirect(base_url() . 'index.php?admin/marks/' . $this->input->post('exam_id') . '/' . $this->input->post('class_id') . '/' . $this->input->post('subject_id'), 'refresh');
}

我指出了我纠正的一条线,这可能是问题所在。您说mark_obtained列没有更新,但您在代码中正在更新名为mark的列。