CodeIgniter Forums
Help with check if user exist - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help with check if user exist (/showthread.php?tid=54890)



Help with check if user exist - El Forum - 09-30-2012

[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);
    }

}



Help with check if user exist - El Forum - 09-30-2012

[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??


Help with check if user exist - El Forum - 09-30-2012

[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');



Help with check if user exist - El Forum - 10-01-2012

[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!


Help with check if user exist - El Forum - 10-01-2012

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