在Codeigniter中刷新相同的URL时,Controller正在从一个视图重定向到另一个视图

时间:2016-01-09 10:24:22

标签: php codeigniter codeigniter-2

我有create_student视图,只需要学生名称并生成自动增量主键id,用户登录后保存user_info会话, 如果存在验证错误,则会渲染create_student视图,否则将从student_model返回最近插入的学生ID 打开学生详细信息的视图,即student_detail视图。

问题是我刷新此网址http://localhost/MyProject/create_student/Create_Student 我从student_detail视图转到第一个视图,即create_student,每当我在student_detail视图重复学生输入时我重新加载此URL 每次都在数据库中保存。请帮助解决我的问题。

  

create_student Controller:

public function Create_Student() {


   $this->form_validation->set_rules('student_title', 'Create Student', 'trim|required|xss_clean');
   $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

   $course_name=$this->input->post('title_title');  
   $user_info=$this->session->userdata('user_info');
   $user_id=$user_info['id'];

   if ($this->form_validation->run() == FALSE) 
   {
    $this->load->view('create_student');
   }
   else
   {

    $data=array('u_id'=>$user_id,'course_title'=>$course_name);
    $result_student = $this->student_model->Create_Student($data);

    if($result_course!=false)
    {
        $data['result'] = $result_student;
        $this->load->view('student_detail',$data);
    }
    else
    {
        $this->load->view('create_student');

    }
   }

}
  

create_student查看:

    <?php echo form_

open('create_student/Create_Student') ?>
  <?php echo validation_errors(); ?>
  <input type="text" class="middle" name="student_title" class="middle"/>
  <button class="middle green" name="submit" type="submit">Create Student</button>
<?php echo form_close();  ?> 
  

Student_Model:

public function Create_Student($data) {

   $this->load->database();
   $this->db->insert('user', $data);
   $insert_id = $this->db->insert_id();
   if ($this->db->affected_rows() > 0) {
   return $insert_id;
   }
   else
   {
   return false;
   }
}

1 个答案:

答案 0 :(得分:1)

在Controller中尝试此编码

http://www.google.com

URL

<?php create_student Controller: public function Create_Student() { $this->form_validation->set_rules('student_title', 'Create Student', 'trim|required|xss_clean'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $course_name=$this->input->post('title_title'); $user_info=$this->session->userdata('user_info'); $user_id=$user_info['id']; if ($this->form_validation->run() == FALSE) { $this->student(); } else { $data=array('u_id'=>$user_id,'course_title'=>$course_name); $result_student = $this->student_model->Create_Student($data); if($result_student) { redirect('create_student/student_details/'.$result_student); } else { $this->student(); } } public function student_details() { $data['result'] = $this->uri->segment(3); $this->load->view('student_detail',$data); } public function student() { $this->load->view('create_student'); } } ?>