2017-08-03 69 views
0

我與代碼的控制器:寄存器形式卸載很好

function register() 
{ 
$this->load->view('backend/register'); 
} 

我有一個表格(register.php)與工作良好。我需要在控制器中加載此代碼:

$names     = $this->input->post('name'); 
    $birthday    = $this->input->post('birthday'); 
    $phones     = $this->input->post('phone'); 
    $emails     = $this->input->post('email'); 
    $passwords    = $this->input->post('password'); 
    $rolls     = '0'; 
      $data['name']  = $names; 
      $data['email'] = $emails; 
      $data['password'] = sha1($passwords); 
      $data['phone'] = $phones; 
      $data['birthday'] = $birthday;  
      //validate here, if the row(name, email, password) is empty or not 
      if($data['name'] == '' || $data['email'] == '' || $data['password'] == '') 
       continue; 

      $this->db->insert('student' , $data); 
      $student_id = $this->db->insert_id(); 

      $data2['enroll_code'] = substr(md5(rand(0, 1000000)), 0, 7); 
      $data2['student_id'] = $student_id; 
      $data2['class_id']  = $this->input->post('class_id'); 
      if($this->input->post('section_id') != '') { 
       $data2['section_id'] = $this->input->post('section_id'); 
      } 
      $data2['roll']   = $rolls[$i]; 
      $data2['date_added'] = strtotime(date("Y-m-d H:i:s")); 
      $data2['year']   = $this->db->get_where('settings' , array(
              'type' => 'running_year' 
             ))->row()->description; 

      $this->db->insert('enroll' , $data2); 
    echo 'alert("Contul a fost creat cu succes!")'; 
    redirect(base_url() . 'index.php?login/' , 'refresh'); 

在控制器的當前窗體中加載的頁面非常好。當我添加上面的代碼時,頁面不再加載。我試圖從控制器中刪除代碼行($ this-> load-> view('backend/register');)+添加了我想要但沒有結果的代碼。 如何結合上面的代碼工作?

+0

閱讀此https://www.codeigniter.com/user_guide/libraries/form_validation.html併爲您的密碼使用http://php.net/manual/en/function.password-hash.php和http: //php.net/manual/en/function.password-verify.php也https://www.codeigniter.com/user_guide/general/controllers.html – user4419336

+0

你說如果用戶註冊他/她應該被重定向到'在將用戶詳細信息插入到數據庫後,登錄頁面顯示註冊頁面? – Regolith

回答

0

如果你指的使用單控制器功能顯示註冊頁面,並使用相同的控制器功能處理和數據插入到數據庫使用以下命令:

function register() 
{ 
    if (isset($_POST['submit'])) { 
     $names = $this->input->post('name'); 
     $birthday = $this->input->post('birthday'); 
     $phones = $this->input->post('phone'); 
     $emails = $this->input->post('email'); 
     $passwords = $this->input->post('password'); 
     $rolls = '0'; 
     $data['name'] = $names; 
     $data['email'] = $emails; 
     $data['password'] = sha1($passwords); 
     $data['phone'] = $phones; 
     $data['birthday'] = $birthday; 
     //validate here, if the row(name, email, password) is empty or not 
     if ($data['name'] == '' || $data['email'] == '' || $data['password'] == '') 
      continue; 

     $this->db->insert('student', $data); 
     $student_id = $this->db->insert_id(); 

     $data2['enroll_code'] = substr(md5(rand(0, 1000000)), 0, 7); 
     $data2['student_id'] = $student_id; 
     $data2['class_id'] = $this->input->post('class_id'); 
     if ($this->input->post('section_id') != '') { 
      $data2['section_id'] = $this->input->post('section_id'); 
     } 
     $data2['roll'] = $rolls[$i]; 
     $data2['date_added'] = strtotime(date("Y-m-d H:i:s")); 
     $data2['year'] = $this->db->get_where('settings', array(
      'type' => 'running_year' 
     ))->row()->description; 

     $this->db->insert('enroll', $data2); 
     echo 'alert("Contul a fost creat cu succes!")'; 
     redirect(base_url() . 'index.php?login/', 'refresh'); 
    } else { 
     $this->load->view('backend/register'); 
    } 

} 

注意:您必須name="submit"在因爲if (isset($_POST['submit']))您的提交按鈕是用來檢查是否有$_POST['submit']價值

只要我做了什麼是

if (isset($_POST['submit'])) { 
    //do form processing and insert data 
}else{ 
    //display register page 
    $this->load->view('backend/register'); 
}