Codeigniter:更新

时间:2016-02-01 17:04:12

标签: php sql codeigniter

我想在我的网站上显示用户的更新个人资料页面。数据库上的更新实际上正在工作,但我希望我的userprofile页面在单击提交按钮后显示更新的数据。我将在这里添加什么D:

  

MemberLoginController.php(控制器)

 <?php defined('BASEPATH') OR exit('No direct script access allowed');

   class MemberLoginController extends CI_Controller {
    public function __construct()
    {
        parent::__construct();

      $this->load->model('MemberLoginModel');

}

public function home(){
    $this->load->view('pages/index');
}
public function userprofile(){
    $this->load->view('member/userprofile');
}
 public function useredit(){
    $this->load->view('member/useredit');
}

public function memberlogin(){
     $this->form_validation->set_error_delimiters('<p class=error>','</p>');
     $this->load->library('form_validation');
     $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_validate_credentials');
     $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');


      if($this->form_validation->run()){

           // Perform Actions after getting valid form inputs
                $data = array(
                'email' => $this->input->post('email'),
                'is_logged_in' => 1
            );
        $this->session->set_userdata($data);

            redirect('index.php/MemberLoginController/members');
        }else
            $this->load->view('pages/index');                
}    

public function members(){
        if($this->session->userdata('is_logged_in')){
             $vis = "hidden";
             $id = $this->session->userdata('id');
             $this->load->model('MemberLoginModel');
             $memberinfo['memberinfo']=$this->MemberLoginModel->getMember($id);
             $this->load->view('member/userprofile',$memberinfo);

        }else{
            redirect('index.php/HomeController/home');
        }            
}

public function edit($id){
    $data = array(
    "action" => base_url('/index.php/MemberLoginController/update/'.$id), 
    "data" => $this->db->get_where('member',array('id'=>$id))   
    );    

    $this->load->view('member/useredit', $data);
}

public function update($id){

    $data = array(
            'memberfname'=>  $this->input->post('memberfname'),
            'memberlname'=>  $this->input->post('memberlname'), 
            'email'=>$this->input->post('email'),
            );
            $this->db->update('member',$data,array('id'=> $id));
            redirect('index.php/MemberLoginController/getMember/'.$id);

}

public function getMember(){
    $this->load->model('MemberLoginModel');
    $memberinfo['memberinfo']=$this->MemberLoginModel->getMember();
    $this->load->view('member/userprofile',$memberinfo);
}

public function validate_credentials(){          
    $this->load->model('MemberLoginModel');          
        if($this->MemberLoginModel->login()){
            echo ("<SCRIPT LANGUAGE = 'JavaScript'>
                window.alert('Login Successfully')
                window.location.href='userprofile'
                </SCRIPT>");
            exit();
            return true;
        }else{
            echo ("<SCRIPT LANGUAGE = 'JavaScript'>
                window.alert('Invalid username or password. Please click LOGIN again.')
                window.location.href='home'
                </SCRIPT>");
            exit();
            //$this->form_validation-     >set_message('validate_credentials','Incorrect Email/Password');
            return false;
         }
  }

 }
 ?>
  

MemberLoginModel.php

<?php class MemberLoginModel extends CI_Model{

public function __construct()
{
    parent::__construct();
}
function login()
{
    $this->db->where('email',$this->input->post('email'));
    $this->db->where('password', md5($this->input->post('password')));


$query = $this->db->get('member');  /*i added 'member' table on db new members*/

       if($query->num_rows()>0)
    {
        foreach($query->result() as $rows)
        {
            //add all data to session
            $newdata = array(
                    'id'            => $rows->id,
                    'memberfname'   => $rows->memberfname,
                    'memberlname'   => $rows->memberlname,
                    'email'         => $rows->email,

                    'logged_in'     => TRUE
               );
        }
            $this->session->set_userdata($newdata);
            return true;            
        }else{
            return false;
        }
 }
public function add_user()
{
    $data = array(
        'memberfname'=>$this->input->post('memberfname'),
        'memberlname'=>$this->input->post('memberlname'),
        'email'=>$this->input->post('email'),
        'password'=>md5($this->input->post('password')),

        );
    $this->db->insert('member',$data);
}

public function getMember()
{
    $query=$this->db->get('member');
    return $query->result();
}    


}
?>
  

Userprofile.php(查看)

    <body>
        <div class="row rowpadding">
            <div class="col-md-3 col-sm-3">                                   
                <div class="user-wrapper">          
                <div class="description">
                <a href="#"><img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg"></a>
                </div>
                <br><br>
                 </div>
            </div>

            <div class="col-md-6 col-sm-6  user-wrapper">
                <div class="description">
                <br>
                <h2 class="name">Hi,    <?php echo $this->session->userdata('memberfname'); ?>!</h2>                    
                <hr/>
            <div class="colwrapper">                    
                <div class="cont-5">
                    <div class="cont-6 name"><p class="para-2"><span class="font-3">First Name:  <?php echo $this->session->userdata('memberfname'); ?>
                    </span></p></div>
                </div><br>
                <div class="cont-7">
                    <div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name: <?php echo $this->session->userdata('memberlname'); ?></span></p></div>
                </div><br>
                <div class="cont-9">
                    <div class="cont-10"><p class="para-4"><span class="font-5">Email Address: <?php echo $this->session->userdata('email'); ?></span></p></div>
                </div><br>
                <div class="cont-11">
                    <div class="cont-12"><p class="para-5"><span class="font-6"></span></p></div>
                </div><br>                  
            </div>
                <br><br>
                </div>                
            </div>                          
         <div class="col-md-3 col-sm-3  user-wrapper">
                <div class="user-wrapper">
                    <br>
                <div class="description">
                  <ul class="rightnavi"style="">
                  <li class="rightnavi"><a href="<?php echo base_url(); ?>index.php/upload/upload">Add Profile Photo</a></li>
                  <li class="rightnavi"><a href = "<?php echo base_url() ?>/index.php/MemberLoginController/edit/<?php echo $this->session->userdata('id'); ?>">Update Your Profile</a></li>
                  </ul>
                  <hr/>
                </div>
                </div>
        </div>
       <!-- USER PROFILE ROW END-->
</div>  
</body>
  

Useredit.php(查看)

<form  action="<?php echo $action;?>" method="POST" enctype="multipart/form-data">
<div class="container">
    <div class="row rowpadding">
            <div class="col-md-3 col-sm-3">

                <div class="user-wrapper">

                <div class="description">
                <a href="#"><img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg"></a>
                </div>

                <br><br>
                 </div>
            </div>

            <div class="col-md-6 col-sm-6  user-wrapper">
                <div class="description">
                <br>

                <h2 class="name">Hi,    <?php echo $this->session->userdata('memberfname'); ?>!</h2>                    
                <hr />
                 <p>
                <div class="colwrapper">

                <div class="cont-5">
                    <div class="cont-6 name"><p class="para-2"><span class="font-3">First Name:  
                    <input type="text" name="memberfname" value="<?php echo $this->session->userdata('memberfname'); ?>" required />
                    </span></p></div>
                </div><br>
                <div class="cont-7">
                    <div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name:
                    <input type="text" name="memberlname" value="<?php echo $this->session->userdata('memberlname'); ?>" required /></span></p></div>
                </div><br>
                <div class="cont-9">
                    <div class="cont-10"><p class="para-4"><span class="font-5">Email Address:
                    <input type="text" name="email" value="<?php echo $this->session->userdata('email'); ?>" required /></span></p></div>
                </div><br>
                <div class="cont-11">
                <div>
                        <input type="hidden" name="hidden" value="<?php echo $this->session->userdata('id'); ?>"/>
                        <input type="submit" value="update">
                </div>
                </div>
                <br><br>
                   </p>
                </div>                
                </div>      
            </div>
    </div>
</div>
</form>

2 个答案:

答案 0 :(得分:0)

重定向应为

redirect('MemberLoginController/members');
redirect('Controller/Method'); 

如果未添加方法,则默认情况下会调用index()方法

答案 1 :(得分:0)

您应该将form_validation对象与您的代码结合使用。

public function update($id)
{
    if ( (int)$id < 1)//$id is not an integer
    {
        redirect('memberlogincontroller/home', 'refresh');
    }
    else
    {
        $this->load->library('form_validation');//it's good to autoload it

        $this->form_validation->set_rules('memberfname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('memberlname', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required');

        if ($this->validation_form->run() === FALSE)
        {
            $this->load->view('userprofile');
        }
        else
        {
            $data = array(
                'memberfname' => $this->input->post('memberfname'),
                'memberlname' => $this->input->post('memberlname'),
                'email' => $this->input->post('email'),
            );

            $this->db->update('member',$data,array('id'=> $id));
            redirect('memberlogincontroller/getMember/' . $id, 'refresh');
        }

    }
}

在模型中,您没有传递特定成员的ID。它应该是这样的

public function getMember($id)
{
    $this->db->where('id', $id);
    $query = $this->db->get('member');

    if ($query->num_rows() !== 1)
    {
        return FALSE;
    }
    return $query->row();//since you need just single member data
}

此外,在控制器方法代码中,您必须将$id传递给model:

public function getMember($id)
{
    if (int($id) < 1)
    {
        redirect('memberlogincontroller/home', 'refresh');//no valid uri segment
    }
    else
    {
        $this->load->model('MemberLoginModel');
        $memberinfo['memberinfo'] = $this->MemberLoginModel->getMember($id);

        if ( ! $memberinfo['memberinfo'])//returned FALSE from model
        {
            redirect('memberlogincontroller/home', 'refresh');//no $id in DB
        }
        else
        {
            $this->load->view('member/userprofile',$memberinfo);
        }
    }
}

另外,您应该注意命名约定。你不应该关闭文件与关闭php标签。我的建议是仔细阅读docs,因为你可以避免许多烦人的错误。