dongzangchui2072 2015-12-10 18:45
浏览 8
已采纳

更新详细信息ci

I am currently trying to allow a user when logged in to update their own details. So far, the user is only allowed to edit user 1 but the form does not populate either. I would like to be able to allow the specific session user to change their details, but don't know what way to go about this. Any guidance would be appreciated, thanks!

Login Controller- 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- 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 view (located in login folder) 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>
  • 写回答

1条回答 默认 最新

  • drllqg2903 2015-12-10 19:29
    关注

    First, you need to pass the data from the Login controller to the view so that you can pre-populate the form fields:

    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);
    }
    

    The view can then access the array elements as individual variables and pre-populate the input fields:

    <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>
    

    To save the user's new data in Login_model.php, just get the id from the session:

    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);              
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c