我尝试编辑用户帐户,但是当我加载视图时,它不会显示用户的默认帐户信息。下面是我的模型视图和控制器。任何帮助将不胜感激。 我有一个数据库,其中包括所有用户信息。所以我想检索它并执行更新功能。
CONTROLLER
/**
* This function is used load user edit information
* @param number $userId : Optional : This is user id
*/
function editOld($userId = NULL)
{
if($userId == null)
{
redirect('pages/settings');
}
$data['userInfo'] = $this->settings_model->getUserInfo($userId);
$data['title'] = 'Settings';
$this->load->view('templates/header');
$this->load->view('pages/settings', $data);
$this->load->view('templates/footer');
}
/**
* This function is used to edit the user information
*/
function editUser()
{
$this->load->library('form_validation');
$userId = $this->session->userdata('user_id');
$this->form_validation->set_rules('name','Full Name','trim|required|max_length[128]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|xss_clean|max_length[128]');
$this->form_validation->set_rules('username','Username','trim|required|xss_clean|min_length[3]');
$this->form_validation->set_rules('birthday','Birthday','required|xss_clean');
$this->form_validation->set_rules('gender','Gender','required|xss_clean');
$this->form_validation->set_rules('mobile','Mobile Number','required|min_length[9]|xss_clean');
if($this->form_validation->run() == FALSE)
{
$this->editOld($userId);
}
else
{
$name = $this->input->post('name');
$username = $this->input->post('username');
$email = $this->input->post('email');
$bio = $this->input->post('bio');
$mobile = $this->input->post('mobile');
$birthday = $this->input->post('birthday');
$gender = $this->input->post('gender');
$userInfo = array();
if(empty($password))
{
$userInfo = array('email'=>$email, 'username'=>$username, 'name'=>$name,
'mobile'=>$mobile, 'bio'=>$bio, 'birthday'=>$birthday, 'gender'=>$gender, 'updatedBy'=>$userId, 'updatedDtm'=>date('Y-m-d H:i:s'));
}
else
{
$userInfo = array('email'=>$email, 'username'=>$username, 'name'=>$name,
'mobile'=>$mobile, 'bio'=>$bio, 'birthday'=>$birthday, 'gender'=>$gender, 'updatedBy'=>$userId, 'updatedDtm'=>date('Y-m-d H:i:s'));
}
$result = $this->settings_model->editUser($userInfo, $userId);
if($result == true)
{
$this->session->set_flashdata('success', 'Profile updated successfully');
}
else
{
$this->session->set_flashdata('error', 'Profile update failed');
}
redirect('pages/settings');
}
}
MODEL
function getUserInfo($userId)
{
$this->db->select('id, name, email, username, mobile, birthday, gender, bio');
$this->db->from('users');
$this->db->where('isDeleted', 0);
$this->db->where('id', $userId);
$query = $this->db->get();
return $query->result();
}
/**
* This function is used to update the user information
* @param array $userInfo : This is users updated information
* @param number $userId : This is user id
*/
function editUser($userInfo, $userId)
{
$this->db->where('id', $userId);
$this->db->update('users', $userInfo);
return TRUE;
}
查看
<?php
$userId = '';
$name = '';
$email = '';
$mobile = '';
$username = '';
$birthday = '';
$bio = '';
if(!empty($userInfo))
{
foreach ($userInfo as $uf)
{
$userId = $uf->id;
$name = $uf->name;
$email = $uf->email;
$mobile = $uf->mobile;
$username = $uf->username;
$birthday = $uf->birthday;
$bio = $uf->bio;
}
}
?>
<form class="col s12" role="form" action="<?php echo base_url() ?>settings/editUser" method="post" id="editUseri" role="form">
<div class="row">
<div class="input-field col s12">
<input id="name" type="text" class="validate" name="name" value="<?php echo $name; ?>">
<input type="hidden" value="<?php echo $userId; ?>" name="userId" id="userId" />
<label for="name">Full Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="username" type="text" class="validate" data-length="15" name="username" value="<?php echo $username; ?>">
<label for="username">Username</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="email" type="text" class="validate" name="email" value="<?php echo $email; ?>">
<label for="email">Email</label>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-primary" value="Submit" />
<input type="reset" class="btn btn-default" value="Reset" />
</div>
</form>
答案 0 :(得分:0)
尝试添加:
$this->db->last_query()
检查查询的内容。
我认为需要设置$query->result();
$this->db->set()
用于处理更新查询的数据部分
答案 1 :(得分:0)
尝试此操作仅用于调试您的错误
您未选中用户会话设置
从
改变if($this->form_validation->run() == FALSE)
{
$this->editOld($userId);
}else{
....
....
}
到
if($this->form_validation->run() == FALSE)
{
//your validation error
echo "validation error";
}else if($userId){
//if session set for user
$this->editOld($userId);
}else{
....
....
}
答案 2 :(得分:0)
您可以将您的详细信息传递到会话中并使用会话来显示它。记得更新会话。
value="<?php echo $this->session->userdata('username'); ?>"
如果适合你,则标记为正确。