• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with check if user exist

#1
[eluser]razorsese[/eluser]
The problem is when someone register it doesn't check if that name already exist in database .So in my user table i have multiple columns with same name.
I have the following code controller->form.php:

Code:
class Form extends CI_Controller
{
    function index()
    {
        $data['id']       = $this->input->post('username');
        $data['password'] = $this->input->post('password');
        $username         = $data['id'];


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


        $this->load->model('form_model');
        $this->load->library('form_validation');
        
        
        
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|callback_check_usr_model[form_model{$username}]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
            $this->db->insert('user', $data);
            
            
            
        }
    }
    
    
    
}

and model->form_model.php:

Code:
class form_model extends CI_Model {


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


    public function check_usr($username)
    {
        
       $this->db->where('id', $username);
           $this->db->from('user');
       return $this->db->count_all_results($this->table);
    }

}

#2
[eluser]ojcarga[/eluser]
[quote author="razorsese" date="1349002467"]
The problem is when someone register it doesn't check if that name already exist in database.
[/quote]

Are you just telling us what your application does not do or are you asking something??

#3
[eluser]Bankzilla[/eluser]
[quote author="ojcarga" date="1349019614"][quote author="razorsese" date="1349002467"]
The problem is when someone register it doesn't check if that name already exist in database.
[/quote]

Are you just telling us what your application does not do or are you asking something??[/quote]

Instead of being a dick you could of just easily suggested a solution.


The database library has something built in that you aren't taking advantage of yet.

Code:
is_unique[user.id]
This will go the end of your validation rule and can go in replace of your callback function.

Code:
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|is_unique[user.id]|xss_clean');

#4
[eluser]boltsabre[/eluser]
Also, after successful form submission you are just loading a new view, don't! use "redirect()" instead, otherwise the $_POST is still available full of the form data. If the user refreshes that page, it tries to submit these post values again!

#5
[eluser]razorsese[/eluser]
Thanks very much.I fixed it.
Thanks again


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.