Hello,

I have tried to create login page with CI. This is the codes that I have:

views/login.php

<html>

<link href="<?php echo $logincss; ?>"  rel="stylesheet" type="text/css" media="screen">

<div id="logoadmin"></div>

<div id="loginbox">

<img src="<?php echo $logo2; ?>" width="150">
<br><br>

<div id="username">
<br><br>
<div id="position">

<?php      

      echo form_open('clogin/process');

      echo form_label('Username:&nbsp&nbsp', 'username');

      $dataUsername = array(
              'username1'   => 'username1',
              'id'          => 'username1',
              'value'       => '',
              'size'        => '10',
              'style'       => 'width:43%'
            );

      echo form_input($dataUsername); ?><br>

<?php      

      echo form_label('Password:&nbsp&nbsp', 'password');

      $dataPassword = array(
              'password'    => 'password',
              'id'          => 'password',
              'value'       => '',
              'size'        => '10',
              'style'       => 'width:43%'
            );

      echo form_password($dataPassword); ?><br>

</div>

<div id="submit">
<?php echo form_submit(array('value' => 'login', 'class' => 'button')); ?>
</div>
</div>

</div>




</html>

controllers/clogin.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login controller class
*/
class Clogin extends CI_Controller{

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

    public function index(){
        // Load our view to be displayed
        // to the user

        $this->data['assets'] = array('logincss' => base_url().'assets/css/login.css',
                                'logo2' => base_url().'assets/images/logo2.png'
                                );

        $this->load->view('login', $this->data['assets']);
    }

     public function process(){

        $username = $this->input->post('username', TRUE);  
        $password = $this->input->post('password', TRUE);

        // Load the model
        $this->load->model('Login_model');
        // Validate the user can login
        $result = $this->Login_model->validate($username, $password); // here you send data to model
        // Now we verify the result
        if(! $result){
            // If user did not validate, then show them login page again
            $this->index();
        }else{
            // If user did validate,
            // Send them to members area
            redirect('admin/admin');
        }        
    }
}
?>

models/login_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login model class
*/
class Login_model extends CI_Model{

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

    public function validate($username, $password){ // same data you have sent from controller

        // Prep the query
        $this->db->get_where('username', $username);
        $this->db->get_where('password', $password);

        // Run the query
        $query = $this->db->get('surveillanceci');
        // Let's check if there are any results
        if($query->num_rows() == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                    'username' => $row->username,
                    'password' => $row->password,
                    'validated' => true
                    );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
}  

?>

When I try to login this is the error that appears:

A Database Error Occurred

Error Number: 1146

Table 'surveillanceci.username' doesn't exist

SELECT * FROM (username) WHERE 0 IS NULL

Filename: C:\xampp\htdocs\surveillanceCI\system\database\DB_driver.php

Line Number: 330

How to fix the error?

Table 'surveillanceci.username' doesn't exist

There is no table in your database with the name "username". Specify the right table name, or create it.

I wonder which code that tells to look for a table name "username" ? Indeed there is no table name "username" only column name "username".

Line 14. when you use the get_where function it's running the info for your DB on that line get_where(table name, where evaluation, limit, offset). Same thing in line 15. If you're trying to actively build your where clause you need to use the where function:

        // Prep the query
        $this->db->where('username', $username);
        $this->db->where('password', $password);

        // Run the query
        $query = $this->db->get('surveillanceci');

I wonder how should I code it?

If this is the formula:

$this->db->where('tablename');
$this->db->get('tablename');

This is my table:

ids_user

username
password
user_email
user_fname
user_address
user_phone

I basically needs to check if the password is the same as the inputed data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.