0

I am trying to pull in the users table and if the email is already in the database, i don't want the user to be able to add another row with the same email. The below code is what I've put together so far but it doesn't seem to work correctly. Does anybody notice what I am doing wrong?

    $email = $this->input->post('email');
    $password = $this->input->post('password');
    $firstname = $this->input->post('firstname');
    $lastname = $this->input->post('lastname');
    $usersAddress = $this->input->post('usersAddress');
    $usersCity = $this->input->post('usersCity');
    $usersState = $this->input->post('usersState');
    $phoneNumber = $this->input->post('phoneNumber');

    $query1 = $this->db->get('users');
    foreach($query1->result() as $row) {    
        if ($row->email == $email) {
            echo 'Sorry but this email is already in use. Please go back and use a different email.';
        } else {
            $data = array('email' => $email,
                          'password' => $password,
                          'firstname' => $firstname,
                          'lastname' => $lastname,
                          'usersAddress' => $usersAddress,
                          'usersCity' => $usersCity,
                          'usersState' => $usersState,
                          'phoneNumber' => $phoneNumber);
            $this->db->insert('users', $data); 
            $this->session->set_userdata('id', $this->db->insert_id());
            $this->session->set_userdata('logged', 'true');
            $this->session->set_userdata('firstname', $firstname);
            $this->session->set_userdata('lastname', $lastname);
            $this->session->set_userdata('email', $email);
            mail($email,"KyPlays.org","You have successfully regstered at KyPlays.org");
            header("Location: ".base_url()."index.php/routers/startpage?requestedPageType=regPart2");
        }
    }
5
  • 2
    don't get all the users, check if the single email address is in the db or not. Commented Dec 20, 2012 at 0:52
  • So change the query or the foreach loop? Commented Dec 20, 2012 at 0:55
  • 1
    both, no need for the loop, check for the email, then do your if\else according to the the result Commented Dec 20, 2012 at 0:56
  • your code does add for each row a new entry if email not match. just think it over @Dogan +1 Commented Dec 20, 2012 at 1:01
  • Okay I got it thanks for your all's help Commented Dec 20, 2012 at 1:03

2 Answers 2

4

No need for the loop. Just check if the email is being used:

$query = $this->db->get_where('users', array('email' => $email));
if ($query->num_rows() > 0) {
  // Show error message as email already exists in the database
} else {
  // Insert new user
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man. Yeah I just got done rewriting it like how you have it written and it works perfectly now.
3

you could also use the built in form_validation and use something like:

$this->form_validation->set_rules('email', 'Email','required|valid_email|is_unique[users.email]');

see also: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.