Welcome Guest, Not a member yet? Register   Sign In
How to compare data from a textfield to data in a mysql db using CodeIgniter?
#1

[eluser]Zombie1[/eluser]
Hello all,

I started using CodeIgniter yesterday afternoon so I'm a newbie at this. I've designed a controller that checks if the username and password are equal to the data inside the db.

The validation works fine if the user doesn't enter anything into them, (required), but for some reason the controller doesn't enter the functions that check the value of the textfield to the value stored inside the db. I'm don't think I'm calling the functions inside the controller correctly.

Here's the code for the controller:

Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form extends CI_Controller {

function index()
{

  // Include helper form and url
  $this->load->helper(array('form', 'url'));

  // Validate form
  $this->load->library('form_validation');
  
  // Validate fields
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  
  $this->load->model('Log_in_db');

  if ($this->form_validation->run() == FALSE)
  {
   // Stay on same page if data is not equal to.
   $this->load->view('log_in');
  }
  else
  {
   // Redirect user to my_blog page.
   $this->load->view('my_blog');
  }
}

function user_not_exists($username) {

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

     $user_check = $this->user_model->user_not_exists($username);

     if($user_check > 0) {
   redirect('my_blog');
   return TRUE;
     }
     else {
   $this->form_validation->set_message('required', 'Username does not match');
         return FALSE;
     }

}

function password_not_exists($password) {

     $check_password = $this->user_model->password_not_exists($password);

     if($check_password > 0) {
   redirect('my_blog');
   return TRUE;
     }
     else {
   $this->form_validation->set_message('required', 'Password does not match');
         return FALSE;
     }

}

}

Here's then code for the model:

Code:
class Log_in_db extends CI_Model {

var $username;
var $password;

function __construct()
     {
         // Call the Model constructor
         parent::__construct();
     }
  
function user_not_exists($username) {

  $username = $_POST['username'];
     $this->db->where('username', $username);
     $query = $this->db->get('users');

     return $query->num_rows();

}

function password_not_exists($password) {

  $password = $_POST['password'];
     $this->db->where('password', $password);
     $query = $this->db->get('users');

     return $query->num_rows();

}

}

I've been trying to find a solution to this for the last couple of hours. Any help would be appreciated.
#2

[eluser]treenef[/eluser]
You are using it incorrectly. You can either call the model function in the success of the form validation function, or create your own function in the form validation bit.
#3

[eluser]Zombie1[/eluser]
Thanks it worked. I split half the code up calling one function inside the success section and then simply calling the model inside that function.
#4

[eluser]treenef[/eluser]
I know this is solved, but take a look at creating your own callbacks

http://ellislab.com/codeigniter/user-gui...#callbacks

This allows you more control and allows you to echo specific error messages. You might want to take a look.
#5

[eluser]CroNiX[/eluser]
I prefer to extend the form_validation library with additional custom rules. That way they can be reused in many controllers without having to copy the callback function to each one that uses that rule.




Theme © iAndrew 2016 - Forum software by © MyBB