视图中的未定义变量

时间:2016-12-18 06:09:49

标签: php codeigniter model-view-controller

我正在尝试在php,Codeigniter中创建一个简单的更新函数表单页面。当我尝试运行我的文件时出现错误:

  

严重性:注意

     

消息:未定义的变量:用户名

     

文件名:views / update_employee_view.php

表示每个变量,不仅仅是用户名。

updateEmployee.php

<?php


class updateEmployee extends CI_Controller {


    public function index()
    {


                $this->load->view('update_employee_view');

    }

    public function signin()
    {
        //$this->load->view('includes/header');
        $this->load->view('update_employee_view');
        //$this->load->view('includes/footer');
    }

    public function validate()
    {   $this->load->helper('email');
        $this->load->library('form_validation');
        $this->load->model('employee_update_model');

        $query=$this->Users->signin_control();

        if($query)
            {   $user_id = $this->Users->get_user_id_from_username();
                $user    = $this->Users->get_user($user_id);

                $data = array('id'=>$user_id, 'email' =>$this->input->post('email') , 'is_logged_in'=>true);

                $this->session->set_userdata($data);
                //$this->load->view('includes/header');
                $this->load->view('update_employee_view',$user);
                //$this->load->view('includes/footer');
            }
        else
            {   

                $this->index();
            }
    }

    public function update()
    {
        $this->load->model('employee_update_model');

        $id=$this->session->userdata('id');
        $email=$this->session->userdata('email');

        $updated=array(
        'password' =>$this->input->post('password'),
        'password' =>$this->input->post('password'),
        'username' =>$this->input->post('username'),
        'employee_emer' =>$this->input->post('employee_emer'),
        'mbiemer' =>$this->input->post('mbiemer'),
        'adresa'=>$this->input->post('adresa'),
        'email'=>$this->input->post('email'),

        );
        $query=$this->employee_update_model->update_profile($id,$email,$updated);

        $data=$this->employee_update_model->show_user_again($id);
        //$this->load->view('includes/header');
        $this->load->view('update_employee_view',$data);
        //$this->load->view('includes/footer');
    }

    /*public function update_password()
    {   
        $this->load->library('form_validation');
        $this->load->model('Users');

        $id=$this->session->userdata('id');

        $old= $this->input->post('old_password');
        $new1= $this->input->post('new_password');
        $new2=$this->input->post('new_password2'); 

        $query=$this->Users->check_old_password($id,$old);
        if($query)
        {   
            $this->form_validation->set_rules('new_password','New Password','trim|required|min_length[5]');
            $this->form_validation->set_rules('new_password2','Confirm Password','trim|required|min_length[5]|matches[new_password]');


        if($this->form_validation->run()==FALSE)
            {   
                $this->session->set_userdata('msg','');
                $data=$this->Users->show_user_again($id);
                //$this->load->view('includes/header');
                $this->load->view('view_user_home',$data);
                //$this->load->view('includes/footer');

            }
        else
            {   
                $query2=$this->Users->update_password($id,$new1);
                $this->session->set_userdata('msg','Successfully updated');
                $data=$this->Users->show_user_again($id);
                //$this->load->view('includes/header');
                $this->load->view('view_user_home',$data);
                //$this->load->view('includes/footer'); 

            }
        }
        else
                $this->session->set_userdata('msg','Incorrect password');

                $data=$this->Users->show_user_again($id);               
                //$this->load->view('includes/header');
                $this->load->view('view_user_home',$data);
                //$this->load->view('includes/footer');


    }

    */

    public function signup()
    {
        //$this->load->view('includes/header');
        $this->load->view('view_signup',array('error' => ' ' ));
        //$this->load->view('includes/footer');
    }

    public function registration_validate()
    {
        $this->load->helper(array('email','url'));
        $this->load->library(array('form_validation','upload'));
        $this->load->model('employee_update_model');

        $config = array(
        'upload_path' => "././uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf",

        );

        $this->form_validation->set_rules('employee_emer','First Name','trim|required|min_length[2]');
        $this->form_validation->set_rules('mbiemer','Last Name','trim|required|min_length[2]');
        $this->form_validation->set_rules('email','E-mail','trim|required|valid_email');
        $this->form_validation->set_rules('password','E-mail','trim|required|min_length[5]');
        $this->form_validation->set_rules('cpassword','Confirm Password','trim|required|min_length[5]|matches[password]');
        $this->form_validation->set_rules('adresa','Address','trim|required');
        $this->form_validation->set_rules('photo','Upload');



        if($this->form_validation->run()==FALSE)
            {
                //$this->load->view('includes/header');
                $this->load->view('view_signup');
                //$this->load->view('includes/footer');
            }
        else
            {   
                /*
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('photo'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('view_signup', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                        $this->load->view('upload_success', $data);
                }
                */
                $result=$this->Users->insert();
                if ($result){
                    //$this->load->view('includes/header');
                    $this->load->view('update_employee_view');
                    //$this->load->view('includes/footer');

                }
            }
    }
}

employee_update_model.php

<?php
class employee_update_model extends CI_Model{

    function __construct(){
        parent::__construct();
    }


     function insert()
     {  
        $info=array(
                            'id'=>NULL,
                            'employee_emer'=>$this->input->post('employee_emer'),
                            'mbiemer'=>$this->input->post('mbiemer'),
                            'email'=>$this->input->post('email'),
                            'password'=>$this->input->post('password'),
                            'adresa'=>$this->input->post('adresa'),

                            '#foto'=>NULL,
                            );
        $query=$this->db->insert('employee', $info);

        return $query;
     }


      function signin_control()
     {
        $this->db->where('email',$this->input->post('email'));
        $this->db->where('password',$this->input->post('password'));
        $query=$this->db->get('employee');
        if($query->num_rows()==1) 
            return $query;
     }

     public function get_user_id_from_username() 
     {
        $email=$this->input->post('email');
        $this->db->select('id');
        $this->db->from('employee');
        $this->db->where('email', $email);
        return $this->db->get()->row('id');

    }

    public function get_user($user_id) 
    {

        $this->db->from('employee');
        $this->db->where('id', $user_id);
        return $this->db->get()->row();

    }

    public function update_profile($id,$email,$updated)
    {
        $this->db->where('id', $id);
        $this->db->update('employee', $updated);

    }
    function show_user_again($id){
        $this->db->select('*');
        $this->db->from('employee');
        $this->db->where('id', $id);
        return $this->db->get()->row();
    }

    function check_old_password($id,$old){

        $this->db->select('password');
        $this->db->from('employee');
        $this->db->where('id', $id);
        $query= $this->db->get()->row('password');
        if ($query==$old)  return true;
        else return false;

    }
    public function update_password($id,$new1){
        $this->db->set('password',$new1);
        $this->db->where('id', $id);
        $this->db->update('employee');
    }


    var $table = 'employee';
    var $column_order = array('employee_emer','mbiemer','adresa','email',null,null); //set column field database for datatable orderable
    var $column_search = array('employee_emer','mbiemer','adresa'); //set column field database for datatable searchable just firstname , lastname , address are searchable
    var $order = array('id' => 'desc'); // default order 



    private function _get_datatables_query()
    {

        $this->db->from($this->table);

        $i = 0;

        foreach ($this->column_search as $item) // loop column 
        {
            if($_POST['search']['value']) // if datatable send POST for search
            {

                if($i===0) // first loop
                {
                    $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }

                if(count($this->column_search) - 1 == $i) //last loop
                    $this->db->group_end(); //close bracket
            }
            $i++;
        }

        if(isset($_POST['order'])) // here order processing
        {
            $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } 
        else if(isset($this->order))
        {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        }
    }

    function get_datatables()
    {
        $this->_get_datatables_query();
        if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->get();
        return $query->result();
    }

    function count_filtered()
    {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function count_all()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    public function get_by_id($id)
    {
        $this->db->from($this->table);
        $this->db->where('id',$id);
        $query = $this->db->get();

        return $query->row();
    }

    public function save($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function update($where, $data)
    {
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }

    public function delete_by_id($id)
    {
        $this->db->where('id', $id);
        $this->db->delete($this->table);
    }

 public function insert_file($filename){
    $data=array(
               'foto'=>$filename
               );
     $this->db->insert('file', data);
     return $this->db->insert_id();            
 }
 function getalldata(){
    $query=$this->db->get('file');
    return $query->result();
 }




}
?>

update_employee_view.php

<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CodeIgniter Update Database Demo | CodeIgniter Update Query</title>
    <!--link the bootstrap css file-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- link jquery ui css-->
    <link href="<?php echo base_url('assets/jquery-ui-1.11.2/jquery-ui.min.css'); ?>" rel="stylesheet" type="text/css" />
    <!--include jquery library-->
    <script src="<?php echo base_url('assets/js/jquery-1.10.2.js'); ?>"></script>
    <!--load jquery ui js file-->
    <script src="<?php echo base_url('assets/jquery-ui-1.11.2/jquery-ui.min.js'); ?>"></script>

</head>
<body>
<br>

<div class="container">
<div class="row">

    <div class="col-md-8 col-md-offset-2">

          <h3 class="form-signin-heading">Your Profile</h3>
          <div id="profile" >
          <?php
          echo form_open_multipart('updateEmployee/update','class="form-horizontal"');
          ?>

          <div class="row">
            <div class="col-md-6">
                  <?php 
                  echo "User Name: ";
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                  echo form_input('username',$username,' class="form-control"');
                  ?>
             </div> 
           </div>   
           </br>
           <div class="row">
            <div class="col-md-6">
                  <?php 
                  echo "Password: ";
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                  echo form_input('password',$password,' class="form-control"');
                  ?>
             </div> 
           </div>   
           </br>
          <div class="row">
            <div class="col-md-6">
                  <?php 
                  echo "First Name: ";
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                  echo form_input('employee_emer',$employee_emer,' class="form-control"');
                  ?>
             </div> 
           </div>   
           </br>

            <div class="row">
            <div class="col-md-6">
                  <?php 
                   echo form_label('Last Name: ','mbiemer');
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                  echo form_input('mbiemer',$mbiemer,' class="form-control"');
                  ?>
             </div> 
           </div> 
           </br>

            <div class="row">
            <div class="col-md-6">
                  <?php 
                   echo form_label('Adresa: ','adresa');
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                   echo form_input('adresa',$adresa,' class="form-control"');
                  ?>
             </div> 
           </div>     
          </br>
          <div class="row">
            <div class="col-md-6">
                  <?php 
                   echo form_label('Email: ','email');
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                   echo form_input('email',$email,' class="form-control"');
                  ?>
             </div> 
           </div>     
          </br>


            <div class="row">
            <div class="col-md-6">
                  <?php 
                   echo form_label('Photo:','foto');
                  ?>
             </div>
            <div class="col-md-6">
                  <?php 
                   echo form_upload('foto','Photo','placeholder="Upload your file here" class="form-control"');
                  ?>
             </div> 
           </div>         
          </div>
          </br>
        <?php
          echo form_submit('submit','Update Profile','class="btn  btn-primary "') ;

          echo form_close();
          //echo validation_errors('<p class="error">');

          ?>
    </div> 


    </div>
</div>
</div>
</body>
</html>

0 个答案:

没有答案