Welcome Guest, Not a member yet? Register   Sign In
Check if exist in database
#1

[eluser]Casperlarsen94[/eluser]
Hi everyone!

I'm quite new in Codeigniter but i'm trying to learn it.

I have a register form, but i would like to check in the database before it goes to insert in my table.

I hope you can help me.

Register Controller
Code:
<?php
class Register extends Controller
{
    function index()
    {
        $data['content'] = 'register_view';
        $this->load->view('template/template', $data);
    }

    function validate()
    {
        $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[40]');
        $this->form_validation->set_rules('club', 'Club', 'required|min_length[3]|max_length[40]');
        $this->form_validation->set_rules('mail', 'Mail', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[40]');

        if($this->form_validation->run() == TRUE){
            
            $this->load->model('register_model');
            $this->register_model->create();
            redirect('login');

        }else{

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

        }
    }
}
?>

Register Model
Code:
<?php
class Register_model extends Model
{
    function create()
    {    
        $date = time();

        $user_data = array(
        'name' => $this->input->post('name'),
        'club' => $this->input->post('club'),
        'mail' => $this->input->post('mail'),
        'password' => md5($this->input->post('password')),
        'date' => $date
        );
        
        $this->db->insert('users', $user_data);
    }
}
?>

I really hope that someone can explain it to me Smile

Thanks.
#2

[eluser]Fireclave[/eluser]
you can write your callback function in your Validation as rule (http://ellislab.com/codeigniter/user-gui...#callbacks) or you can write a function in the model like $this->register_model->check_available() and you can check the name or/and the email.

You are welcome
#3

[eluser]smilie[/eluser]
Hi,

In your model:

Code:
<?php
class Register_model extends Model
{
    function create()
    {
        # Check if mail exists (I pressume e-mail is unique value in the DB)
        $query = $this->db->get_where('users',array('mail'=>$this->input->post('mail'));
        if($query->num_rows() > 0)
        {
           # E-mail is found in the database
           return FALSE;
        }
        else
        {
           # E-mail not found, write record
           $date = time();

           $user_data = array(
           'name' => $this->input->post('name'),
           'club' => $this->input->post('club'),
           'mail' => $this->input->post('mail'),
           'password' => md5($this->input->post('password')),
           'date' => $date
           );
        
           $this->db->insert('users', $user_data);
         }
    }
}
?>

Cheers,
Smilie
#4

[eluser]Casperlarsen94[/eluser]
Thanks guys! Big Grin
#5

[eluser]Fireclave[/eluser]
You can use librarys like

http://codeigniter.com/wiki/Ion_Auth_-_L...th_System/

This is very nice and helps you with login, regist, logout, forgotten password, etc.

try this out ;-)




Theme © iAndrew 2016 - Forum software by © MyBB