Welcome Guest, Not a member yet? Register   Sign In
before add user, check if this login (email etc...) already exists in database
#1

[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
#2

[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.';
}
#3

[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
#4

[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.
#5

[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-gui...#callbacks




Theme © iAndrew 2016 - Forum software by © MyBB