codeigniter:更新功能模型和控制器

时间:2018-02-26 13:06:46

标签: php mysql codeigniter-2

我不知道我在做错了什么。 如果有人可以帮忙! 因为我尝试了所有解决方案...... 还有未定义的变量$data error

          /************************/                                                             
public function edit_stud($student_id)
{

    $this->form_validation->set_rules('full_name', 'full_name', 'required');
    $this->form_validation->set_rules('father_name', 'father_name', 'required');
    $this->form_validation->set_rules('roll_no', 'roll_no', 'required');


    if($this->form_validation->run() === FALSE)
    {
        $data['student']= $this ->stud_model->update_student($student_id, $data);
        $this->load->view('edit_form' , $data);
    }
    else
    {

    $full_name = $this->input->post('full_name', true);
    $father_name = $this->input->post('father_name', true);
    $roll_no = $this->input->post('roll_no', true);

    $data = array(


    'full_name'=>$full_name ,
    'father_name'=>$father_name ,
    'roll_no'=>$roll_no,



    );

    $student_id = $this->stud_model->update_student($student_id,$data);
    redirect('/stud/edit_stud/'.student_id);

    }
}

// *********************** CI中的模型*** //////////////

public function update_student($student_id,$data)
{
    $this->db->set($this->table_name,$student_id, $data);
    return $this->db->update_id();
}

2 个答案:

答案 0 :(得分:1)

$ IF数据从未在IF语句的第一部分中定义。

特别是这一行

$data['student'] = $this->stud_model->update_student($student_id, $data);

是问题所在。如果表单验证为false,则您不想更新学生。删除该行,一切都应该没问题。

除非您确实想要更新学生,即使验证失败(这是一个坏主意,IMO)。然后你的控制器看起来像这样

public function edit_stud($student_id) {

    $this->form_validation->set_rules('full_name', 'full_name', 'required');
    $this->form_validation->set_rules('father_name', 'father_name', 'required');
    $this->form_validation->set_rules('roll_no', 'roll_no', 'required');

    $full_name = $this->input->post('full_name', true);
    $father_name = $this->input->post('father_name', true);
    $roll_no = $this->input->post('roll_no', true);

    $data = array(
        'full_name' => $full_name ,
        'father_name' => $father_name ,
        'roll_no' => $roll_no,
     );

    $student_id = $this->stud_model->update_student($student_id, $data);

    if($this->form_validation->run() === FALSE)
        $this->load->view('edit_form' , $data);
    } else {
       redirect('/stud/edit_stud/'.student_id);
    }
}

答案 1 :(得分:0)

感谢您提出的解决我遇到的问题的建议 在我的代码中,我做了一些演绎...发布正确的答案 其他人,有同样的问题。 这是控制器代码。

/*****controller code*****/
/***************************edit student ******************************/
    public function edit_stud($student_id)
    {
        $this->form_validation->set_rules('full_name', 'full_name', 'required');
        $this->form_validation->set_rules('father_name', 'father_name', 'required');
        $this->form_validation->set_rules('roll_no', 'roll_no', 'required');
        if($this->form_validation->run() === FALSE)
        {
            $data['student']= $this ->stud_model->get_student($student_id);
            $this->load->view('edit_form' , $data);
        }
        else
        {
        $full_name = $this->input->post('full_name', true);
    $father_name = $this->input->post('father_name', true);
        $roll_no = $this->input->post('roll_no', true);
          $data = array(
        'full_name'=>$full_name ,
        'father_name'=>$father_name ,
        'roll_no'=>$roll_no,
        );
        $student_id = $this->stud_model->update_student($student_id,$data);
        redirect('/stud/edit_stud/'.student_id);    
        }
    }

    /************************delete student ************************/
    public function delete_stud($student_id)
    {
         $this ->stud_model->delete_student($student_id);
         redirect('/stud/');
    }
/***********************model code *******/
/************to get student id*********/
public function get_student($student_id)
    {
        $this->db->select('*');
        $this->db->from($this->table_name);
        $this->db->where('id',$student_id);
        $query = $this->db->get();
         if($query->num_rows() >0)
         {
             $return =  $query->row();
         }
        else
        {
            $return = 0;
        }
        $query->free_result();
        return $return;
    }
/*****************update function***********************/
    public function update_student($id, $data)
    {
        $where = array('id' => $id);
        $this->db->update($this->table_name, $data, $where);
    }