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