我目前正在尝试允许用户在登录时更新自己的详细信息。到目前为止,用户只能编辑用户1,但表单也不会填充。我希望能够允许特定会话用户更改其详细信息,但不知道该怎么做。任何指导都将不胜感激,谢谢!
登录控制器 - Login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('login_model');
}
public function index()
{
if(($this->session->userdata('user_name')!=""))
{
$this->welcome();
}
else {
$data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/signin', $data);
$this->load->view('templates/footer');
}
}
public function welcome()
{
$data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/welcome', $data);
$this->load->view('templates/footer');
}
public function login()
{
$email=$this->input->post('email');
$password=$this->input->post('pass');
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Your Email', 'trim|required');
$this->form_validation->set_rules('pass', 'Password', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else{
$this->login_model->login($email,$password);
$this->welcome();
}
}
public function logout()
{
$newdata = array(
'id' =>'',
'username' =>'',
'email' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
session_destroy();
redirect('login/index');
}
function update()
{ $data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/update', $data);
$this->load->view('templates/footer');
$data = array (
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$this->load->model('login_model');
$this->login_model->update($data);
}
}
?>
登录模型 - Login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function login($email, $password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("mvc_user");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'id' => $rows->id,
'username' => $rows->username,
'email' => $rows->email,
'password' => $rows->password,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
function update($data)
{
$this->db->where('id', 1);
$this->db->update('mvc_user', $data);
}
}
?>
更新视图(位于登录文件夹中)update.php
<div class="six columns">
<?php echo form_open('login/update'); ?>
<p>
<label for="user_name">Username</label>
<input type="text" name="user_name" id="user_name" />
</p>
<p>
<label for="user_email">Email</label>
<input type="text" name="user_email" id="user_email" />
</p>
<p>
<label for="user_password">Password</label>
<input type="text" name="user_password" id="user_password" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
</div>
答案 0 :(得分:1)
首先,您需要将数据从Login控制器传递到视图,以便您可以预先填充表单字段:
function update() {
// Prepare data to pass to the view
$data = array (
'title' => 'MVC Application',
'username' => $this->session->userdata('username'),
'email' => $this->session->userdata('email'),
'password' => $this->session->userdata('password')
);
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/update', $data);
$this->load->view('templates/footer');
$data = array (
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$this->load->model('login_model');
$this->login_model->update($data);
}
然后,视图可以作为单个变量访问数组元素并预先填充输入字段:
<div class="six columns">
<?php echo form_open('login/update'); ?>
<p>
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo $username; ?>" />
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</p>
<p>
<label for="password">Password</label>
<input type="text" name="password" id="password" value="<?php echo $password; ?>" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
</div>
要在Login_model.php中保存用户的新数据,只需从会话中获取ID:
function update($data) {
$my_id = $this->session->userdata('id');
if($my_id !== false) { // Just making sure we're logged in
$this->db->where('id', $my_id);
$this->db->update('mvc_user', $data);
}
}