2016-03-28 86 views
0

我已經使用Codeignitor編寫了用於更新用戶詳細信息的代碼,但是當點擊更新按鈕用戶帳戶頁面時沒有任何事情發生。誰能幫我找到問題和解決方案通過如下因素我下面的代碼來解決:更新Codeigniter中的用戶詳細信息

控制器:

public function update_userdetail() { 
    // Load form helper and validation library 
    $this->load->helper('form'); 
    $this->load->library('form_validation'); 
    // Update field validation 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|is_unique[users.username]', array('is_unique' => 'This username already exists.')); 
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]', array('is_unique' => 'This email already exists.')); 
    if($this->form_validation->run() == false) { 
     redirect('user/account'); 
    } else { 
     // Set variable from Form 
     $user_id = $this->input->post('user_id'); 
     $username = $this->input->post('username'); 
     $email  = $this->input->post('email'); 
     if($this->user_model->update_user($user_id, $username, $email)) { 
      $this->session->set_flashdata('notice','<div class="success">Your details updated Successfully!</div>'); 
      redirect('user/account'); 
     } else {  
      $this->session->set_flashdata('msg', '<div class="error">Problem with update your detail!</div>'); 
      redirect('user/account'); 
     } 
    } 
} 

型號:

public function update_user($username, $email, $user_id) { 
     $data = array (
      'username' => $username, 
      'email'  => $email, 
      'updated_at' => date('Y-m-j H:i:s'), 
     ); 
     $this->db->where('id', $user_id); 
     $this->db->update('users', $data); 
     return; 
    } 

形式:

<?php 
    $username = ($this->session->userdata['username']); 
    $email  = ($this->session->userdata['email']); 
    $user_id = ($this->session->userdata['user_id']); 
    ?> 
    <h3>Update detail</h3> 
    <?php echo $this->session->flashdata('msg'); ?> 
    <?php echo form_open(); ?> 
    <input type="text" id="user_id" name="user_id" value="<?php echo $user_id; ?>"> 
    <div> 
    <input type="text" id="username" name="username" placeholder="Username" value="<?php echo $username; ?>"> 
    <small class="error-text"><?php echo form_error('username'); ?></small> 
    </div> 
    <div> 
    <input type="email" id="email" name="email" placeholder="Enter your email" value="<?php echo $email; ?>"> 
    <small class="error-text"><?php echo form_error('email'); ?></small> 
    </div> 
    <div><?php echo form_submit('submit', 'Submit'); ?></div> 
    <?php echo form_close(); ?> 
+0

cehck此鏈接:https://ellislab.com/codeigniter/user-guide/helpers/form_helper。 html –

+0

ist all check'print_r($ _ POST);'獲取值還是不是? – devpro

+0

@PathikVejani是的,我做了,但無法找到問題出在哪裏:( –

回答

0

我發現這個問題,這是對模型和應該如下:

public function update_user($user_id, $username, $email) //Change effect 
{ 
     $data = array (
      'username' => $username, 
      'email'  => $email, 
      'updated_at' => date('Y-m-j H:i:s'), 
     ); 
     $this->db->where('id', $user_id); 
     return $this->db->update('users', $data); //Change effect 
    } 
0

您需要從autoload.php文件中加載表單助手類,如下所示:

地點:(應用程序/配置/ autoload.php)

$autoload['helper'] = array('form'); 

而第二個解決方案是,加載你的表單輔助函數在控制器的構造函數。

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper('form'); 
} 
+0

但不是結果 –

+0

@RahamathullahMohamedKasim:你用簡單的html表單來試試嗎?像'

>'而不是'<?php echo form_open(); ?> – devpro

+0

是的,我沒有發生任何事情 –