CodeIgniter Forums
before add user, check if this login (email etc...) already exists in database - 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: before add user, check if this login (email etc...) already exists in database (/showthread.php?tid=15641)



before add user, check if this login (email etc...) already exists in database - El Forum - 02-10-2009

[eluser]Unknown[/eluser]
Hi there

This is my first post here, but I think not last Smile

Now i am creating code to register new users at site. It puts some values like user_name, password, email in database.

I wonder if CI has some functions/classes/helpers etc to check automaticly if in database i have already thesame user_name and/or email? Or i need to write code to check it myself


before add user, check if this login (email etc...) already exists in database - El Forum - 02-10-2009

[eluser]JWarren[/eluser]
I don't know if CI has something built in, but it doesn't take much to add a function to a model to do it.

quick example:
Code:
function email_registered($email) {
    $query = $this->db->query("Select * from users WHERE email = ?", array($email));
    return $query->num_rows();
}

Code:
if ($this->model_name->email_registered($_POST['email']) > '0') {
    $error_message = 'Already registered.';
}



before add user, check if this login (email etc...) already exists in database - El Forum - 02-10-2009

[eluser]Unknown[/eluser]
Ok, thank you very much!

btw> i see that it should be (first code) in Models, right? I still don't undertstand well MVC idea


before add user, check if this login (email etc...) already exists in database - El Forum - 02-10-2009

[eluser]JWarren[/eluser]
I keep any functions that read from or write to a database in a model. Then load the model in a controller and call it's functions in the controller or view. It has done wonders to keep my code much more organized.


before add user, check if this login (email etc...) already exists in database - El Forum - 02-10-2009

[eluser]internut[/eluser]
Form validation has a callback function you can use as well to do a SQL lookup to check if it exists.

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks