尝试将数据提取到我的仪表板登录,codeigniter时出错

时间:2017-04-08 18:51:33

标签: php codeigniter fetch

我已经建立了一个登录系统,它运行正常..它打开了一个不同的页面,具体取决于用户名和&写的密码对应管理员或简单用户。

看看:

enter image description here

但我想在"用户页面添加一个表格"我收到了这个错误:

enter image description here

行错误:

enter image description here

不知道该怎么做:/。

这是我的完整代码:

我的登录视图文件(" login_form"):

        </head>

        <body>
            <div class="container">
            <h3>Login</h3>
            <hr>
            <form action="<?php echo base_url('login/do_login')?>" method="POST">
                <div class="form-group">
                    <label for="cari">USERNAME</label>
                    <input type="text" name="usuario" id="usuario" class="form-control">

                </div>

                <div class="form-group">
                    <label for="cari">PASSWORD</label>
                    <input type="password" name="contrasena" id="contrasena" class="form-control">

                </div>

                <input class="btn btn-primary" type="submit" value="Login" name="login">
                <input class="btn btn-primary" type="reset" value="Reset">

            </form>
                </div>



        </body>
</html>

我的控制器文件(&#34;登录&#34;):

    <?php

        Class Login extends CI_Controller{


        public function index(){


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


        }

 public function do_login()
        {
         // load the form_validation library
         $this->load->library('form_validation');

         $this->form_validation->set_rules('usuario', 'Username', 'trim|required|min_length[3]|alpha_numeric');
         $this->form_validation->set_rules('contrasena', 'Password', 'trim|required|min_length[6]');

           // if there is errors
         if ($this->form_validation->run() == FALSE) { 
            // this will load your form with the errors

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

         } else {
           // if no errors we will hit the database
            $user=$this->input->post('usuario', true);
            $pass=$this->input->post('contrasena', true);
            $cek = $this->m_login->proceso_login($user,$pass);
            $hasil=count($cek);

            if($hasil > 0){

                $pelogin =$this->db->get_where('usuarios',array('username' => $user, 'password' => $pass))->row();

                if($pelogin ->type == 0){
                    redirect('login/admin');
                }

                else{
                    redirect('login/usuario');
                }
            }
            redirect('login/index');
        }
    }


            public function home(){

                $data['records']=$this->m_login->getDetails();
                $this->load->view('usuario',$data);
            }

我的模型文件(&#34; m_login&#34;):

        public function getDetails()
        {
            $st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
                ->join('usuarios','usuarios.id=cursadas.user_id')
                ->join('materias','materias.id=cursadas.subject_id')
                ->WHERE('cursadas.user_id=',$this->session->userdata['id'])
                ->get()->result_array();
            return $st[0]; // or use the row function
        }

和我的信息中心&#34; usuario&#34; (用户页面):

                <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <thead>
                    <th>id</th>
                    <th>User</th>
                    <th>Subject</th>
                    <th>Grade</th>
                    <th>Date</th>
                </thead>


<tbody>
    <?php

    if (count($records) > 0) {
        foreach($records as $record) {

            echo "<tr>
                      <td>".$record['id']."</td>
                      <td>".$record['User']."</td>
                      <td>".$record['name']."</td>
                      <td>".$record['grade']."</td>
                      <td>".$record['date']."</td>
                  </tr>";
        }

       }
    ?>

</tbody>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

如果用户是admin类型,那么它将进入管理页面,如果用户正常,那么它将转到包含其所有数据的用户页面。当用户类型不是admin时,您不会发送任何用户数据。生成通知是因为当用户不是admin时,usuario页面上没有$ records 在控制器中为admin和usuario方法创建具有相应数据的管理方法

您的登录控制器:  添加这些方法

 // for admin data 
 public function admin (){

  $data['records']=$this->m_login->getDetails();
  $this->load->view('admin_view_page',$data);
}
// for user data 
public function usuario(){

    $data['records']=$this->m_login->getDetails();
    $this->load->view('usuario',$data);
}

修改do_login方法,如下所示:

if($hasil > 0)
{

   $pelogin =$this->db->get_where('usuarios',array('username' => $user, 'password' => $pass))->row();
  // here $pelogin has the id of the user 
 // create session like this 
  $this->session->set_userdata(array('id' => $pelogin->id));
//  OR  like this 
// $this->session->set_userdata('id', $pelogin->id);

  if($pelogin ->type == 0)
  {
    // here goes  the admin data 
     redirect('admin');
  }

  else{
           //call here usuario method which has user data who logged in like
             redirect('usuario');
          // OR
          // Call some method which has user data in $records and 
      }
}

答案 1 :(得分:0)

您在视图上有变量$records但在控制器上没有变量

更改

$data['record'] = $this->m_login->getDetails();

// add this array() just in case no results found 

$data['records'] = array(); 
$data['records'] = $this->m_login->getDetails();

$this->load->view('usuario', $data);
  

另一种方法是在控制器上

public function home() {

$results = $this->m_login->getDetails();

$data['records'] = array(); 

if ($results) {
   foreach ($results as $result) {
      $data['records'][] = array(
         'id' => $result['id'],
         'User' => $result['User'],
         'name' => $result['name'],
         'grade' => $result['grade'],
         'date' => $result['date']
      );
   }
}

$this->load->view('usuario',$data);

}

查看usuario.php

<?php if ($records) {?>
<?php foreach($records as $record) {?>
<tr>
<td><?php echo $record['id'];?></td>
<td><?php echo $record['User'];?></td>
<td><?php echo $record['name'];?></td>
<td><?php echo $record['grade'];?></td>
<td><?php echo $record['date'];?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr> 
<td>No Results Found</td>
</tr>
<?php } ?>

只是使用php hash

的例子
  

提示不要将计划密码存储在db varchar 上的数据库哈希密码列中   的 255

<?php

class M_login extends CI_Model {

   public function create_user() {
     $options = [
        'cost' => 12,
     ];

     $new_password = password_hash($this->input->post('password', true), PASSWORD_BCRYPT, $options);

     $data = array(
      'username' => $this->input->post('username', true),
      'password' => $new_password
     );

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

   public function getpassword($username) {
      $this->db->where('username', $username);
      $query = $this->db->get('user');
      if ($query->num_rows() > 0) {
        return $query->row()->password;
      }
   }

 }

然后在登录时验证密码()使用回调

<?php 

class Login extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->library('form_validation');
  $this->load->model('m_login');
}

public function index(){
   $this->do_login();
}

public function do_login() {
  $data['somthing'] = 'Hello';


  $this->form_validation->set_rules('username', 'username', 'trim|required');
  $this->form_validation->set_rules('password', 'password', 'trim|required|callback_verify_password');

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

       $this->load->view('login_form', $data);

    } else {
       // load success stuff

       /*

          Make sure base url is set in config.php something like

          $config['base_url'] = 'http://localhost/projectname/'; 

          End it with /

       */
       redirect(base_url('login/home')); 
    }
}

public function verify_password() {
    // true enables xss_clean
    $stored_hashed = $this->m_login->getpassword($this->input->post('username', true));
    $password = $this->input->post('password', true);

    if (password_verify($password, $stored_hashed)) {
        return TRUE;
    } else {
       $this->form_validation->set_message('verify_password', 'Sorry incorrect username or password');
       return FALSE;
    }
}

public function home() {

$results = $this->m_login->getDetails();

$data['records'] = array(); 

if ($results) {
   foreach ($results as $result) {
      $data['records'][] = array(
         'id' => $result['id'],
         'User' => $result['User'],
         'name' => $result['name'],
         'grade' => $result['grade'],
         'date' => $result['date']
      );
   }
}

$this->load->view('usuario',$data);

}


}