【问题标题】:Codeigniter Database Update IssueCodeigniter 数据库更新问题
【发布时间】:2016-10-12 14:35:21
【问题描述】:

我想更新我的数据,但列 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" >

请帮我解决问题。

【问题讨论】:

标签: php sql database codeigniter


【解决方案1】:
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 的列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2011-12-03
    • 2023-03-11
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多