Email already exists in database Codeigniter - php

Hey :) So I'm trying to check if an user has already registered or not with their email address. The email is a unique field in the database. The problem I am having is when I am checking it, (if the email exists in the table it will return false and a message will be send to the user), num_rows() always returns false even if the email entered does not exist in the table;
I don't know if there is a problem with the post, but if I comment out the email part and register, it will work and if the email is a duplicate, the 1062 error will show.
the model funtion checkEmail()
$email_address = $this->input->post('email');
$this->db->where('email', $email_address);
$result = $this->db->get('user');
if($result->num_rows() > 0){
/*
* the email already exists
* */
return false;
}
and the controller:
$checkEmail = $this->f_model->checkEmail();
if(!$checkEmail){
/*
* if email exists
* */
$msg = '<font color=red>Email already registered.</font><br />';
$this->register($msg);
}
else {
$interest = $this->f_model->enter_register_details_01();
if(!$interest) {
$msg = '<font color=red>Password and Confirm Password do not match.</font><br />';
$this->register($msg);
}
else {
$data['msg'] = $msg;
$this->load->view("registration_view_02", array('interest' => $interest,
'message' => $data));
}
}
even if the table is empty, the message with "Email already registered" appears
Thank for your help.

in checkEmail() function add an else statement
$email_address = $this->input->post('email');
$this->db->where('email', $email_address);
$result = $this->db->get('user');
if($result->num_rows() > 0){
/*
* the email already exists
*
*/
return false;
}else{
return true;
}

Related

PHP/MySQL - Email validation not working

I know this has been asked a lot, and I've search and literally tried it all. I'm really close, but can't get it to work.
I'm trying to check if the username or usermail is already in use and display the error accordingly.
<?php
if (isset($_POST['register'])) {
try
{
$checkValidity = $connect->prepare('SELECT username, usermail FROM users WHERE username = :username OR usermail = :usermail');
$checkValidity->bindValue(':username', $username);
$checkValidity->bindValue(':usermail', $usermail);
$checkValidity->execute();
$row = $checkValidity->fetchColumn();
if ($row == $username) {
$errName = 'This username is already taken.';
}
if ($row == $usermail) {
$errMail = 'Email already in use.';
}
}
catch(PDOException $e)
{
// print $e->getMessage();
}
}
?>
It works fine for the username, but for the life of me I can't get the email error to work...
I attempted separate SELECT queries (username/usermail), and the email error would show ONLY if the username error showed.
The email error will never show just by itself.
I've spent one too many hours just trying to get this to work, and I'm slowly losing my mind.
Any indication would be greatly appreciated...!
Thank you...
FetchColumn return first column value. You need to use fetch instead. Like
$row = $checkValidity->fetch();
if ($row['username'] == $username) {
$errName = 'This username is already taken.';
}
if ($row['usermail'] == $usermail) {
$errMail = 'Email already in use.';
}
There is no way to return another column from the same row if you use PDOStatement::fetchColumn() to retrieve data.
your fetching username only you need to use fetch() to get the whole row and compare like this
$row = $checkValidity->fetch();
if ($row['username'] == $username) {
$errName = 'This username is already taken.';
}
if ($row['useremail'] == $useremail) {
$errMail = 'Email already in use.';
}

Email already exists

Alright so for some reason, Im always getting that email already exists if it doesnt.
public function emailExists($mail) {
$handler = new sql();
$sql = $handler->connect();
$sql->real_escape_string($mail);
$result = $sql->query("SELECT email FROM users WHERE email='".$mail."'");
if($result->num_rows != 0) return true;
else {
$handler->log_write($mail, "register_fail","NULL");
return false;
}
$sql->close();
return false;
}
Now here is the check
if($user->emailExists() == false) {
$user->create($name, $pass, $email, $age, $gender);
jquery_alert("You have been registered. Thank you for using our services. Enjoy your stay!");
jquery_reload();
}
else {
jquery_alert("This email already exists");
}
Pass email address as argument in method.
if($user->emailExists($email) == false) {
// your code here
}
In your function your returning default value is false
And Also Pass email address in your method.
Try this solution
if($user->emailExists($email) == true) {
$user->create($name, $pass, $email, $age, $gender);
jquery_alert("You have been registered. Thank you for using our services. Enjoy your stay!");
jquery_reload();
}
else {
jquery_alert("This email already exists");
}

PHP Script that checks if email exists in table [duplicate]

This question already has answers here:
How to check if a row exists in MySQL? (i.e. check if username or email exists in MySQL)
(4 answers)
Closed 7 years ago.
I'm making a PHP Newsletter script, I'm not very experienced in code, but I try my best to improve, I just need a few ideas in order to make this work.
function validate(){
if(isset($_POST['email'])){
$email = $_POST["email"];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<br>Va rugam introduceti o adresa valida de email";
}else{
return 1;
}
}
}
function checkmail(){
if(validate()==1){
if(isset($_POST['email'])){
$email = $_POST['email'];
$sql = "SELECT * FROM subscribe WHERE email LIKE '$email'";
$connect = new mysqli("localhost", "root", "", "alexandru");
$result = mysqli_query($connect,$sql);
echo print_r($result);
}
}
}
I don't know how I can check the result of the query, I need some ideas, thanks
I have made this simple function which you can use.
function field_exists($field_name, $field_value, $table)
{
global $conn;
try
{
$s = $conn->prepare("SELECT * from $table where $field_name = :f_value");
$s->bindParam(':f_value', $field_value);
$s->execute();
if($s->rowCount() > 0)
{
return true;
}
else
{
return false;
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}//function
Using this function, you can check any table for any value of the specified column.
So in your case, the $field_name would be email, $field_value would be $email and table would be subscribers.
Usage
if(field_exists("email", $email, "subscribers"))
{
//email exists
}
else
{
//email doesn't exist
}
The function would return true if this email in the table exists, and false if the email doesn't.
This code i use whithout oops concept in my practicle ithink it'll help
extract($_POST);
$qwe = "SELECT * FROM user_info where email= '$email'";
$result = mysqli_query($con, $qwe);
if (mysqli_fetch_row($result) >= 1) {
header("Location: userreg.php?err=msg");
}
else {
// query for what u want after check mail doesn't exist
}

Doesnot receive an error

I've an member system now I've made when somebody register an account, he need to activate it by his email. He will receive an valid link in his inbox So for example:
activate.php?email=ipoon2#outlook.com&email_code=b5b90ae21e31229878d681680db16bdf This link is valid so when I go to this link, he activates the account succesfully.
You see after ?email= ipoon2#outlook.com So when I change that into ipodn2#outlook.com and the email_code is still the same, he cannot activate his account. He needs to receive an error like We cannot find that email, and when he changes the email_code He will receive an error like this problem activate your account
Thats the problem what I've got When I change the email I don't receive any error. Neither for email_code
I've a file that is called activate.php which this code is including:
<?php
} else if (isset($_GET['email'], $_GET['email_code']) === true) {
$email = urldecode(trim($_GET['email']));
$email_code = trim($_GET['email_code']);
$user = new User();
if(User::email_exists($email) === false) {
echo 'We cannot find that email'; // return error doesn't show up
} else if (User::activate($email, $email_code) === false) {
echo 'problem activate your account'; // return error doesn't show up
}
}
?>
Also I've 2 functions made, there are in the class file User.php
public function email_exists($email) {
require './config.php';
$email = urldecode(trim($_GET['email']));
$sql_30 = $db->query("SELECT COUNT(id) FROM users WHERE email = '$email'");
if ($sql_30->fetch_object() === true) {
return true;
} else if ($sql_30->fetch_object() === false) {
return false;
}
}
public function activate($email, $email_code) {
require './config.php';
$email = urldecode($email);
$email_code = $db->real_escape_string($email_code);
$sql_33 = $db->query("SELECT COUNT(`id`) FROM `users` WHERE `email` = '$email' AND `email_code` = '$email_code' AND `group` = 0");
if ($sql_33->fetch_object()) {
$db->query("UPDATE `users` SET `group` = 1 WHERE `email` = '$email' AND `email_code` = '$email_code'");
return true;
} else {
return false;
}
}
To me, your email_exists() and activate() are wrong.
if ($sql_30->fetch_object() === true) {
return true;
} else if ($sql_30->fetch_object() === false) {
return false;
}
From the php documentation of mysqli_result::fetch_object :
Returns an object with string properties that corresponds to the fetched row or NULL if there are no more rows in resultset. So your test must be :
if ($sql_30->fetch_object() !== NULL) {
return true;
}
return false;
I guess it should solve your problem.

Joomla 1.5 password check

I've inherited a joomla 1.5 site that has a reset password component that isn't functioning optimally. ATM I'm able to have password resets sent to the user inputted email, but the component is missing a function that checks against existing users to see if the email is valid. I'm rather new to PHP, so I'm not 100% sure how to introduce an additional if statement.
Here's how's it looking so far:
public function submitemail() {
$db = JFactory::getDBO() ;
$requestedEmail = JRequest::getVar('emailaddr' , '') ;
$db->setQuery('select id , username, name, email from #__users where block = 0 and email = "'.$requestedEmail.'"') ;
if( $user = $db->loadObject() ) {
// Make sure the user isn't a Super Admin.
$juser = JFactory::getUser($user->id) ;
//joomla 1.6 and 1.5 check
if ($juser->authorize('core.admin') || $juser->usertype == 'Super Administrator') {
$this->setRedirect( 'index.php?option=com_resetpassword' , 'Email is not valid' ) ;
}
else {
$result = $this->sendPasswordResetEmail( $user ) ;
$this->setRedirect( 'index.php?option=com_resetpassword&layout=success' //, 'Please check your email and follow the instructions to reset your password '
) ;
}
}
else {
$this->setRedirect( 'index.php?option=com_resetpassword' );
}
}
And I happened across a related post where I found this snippet. How would I go about checking against email addresses that are currently in my database against the user inputted email for the reset?
function validate()
{ jimport('joomla.mail.helper');
$valid = true;
if ($this->_data->email && !JMailHelper::isEmailAddress($this->_data->email))
{
$this->_app->enqueueMessage(JText::_('Invalid Email Address'),'error');
$valid = false;
}
return $valid;
}
That is actually exactly what it is doing already. The top loads the row for the user based on the email address that is submitted:
$requestedEmail = JRequest::getVar('emailaddr' , '') ;
$db->setQuery('select id , username, name, email from #__users where block = 0 and email = "'.$requestedEmail.'"') ;
if( $user = $db->loadObject() ) {
...
All you likely need to do is add a message for when this fails to the else statement at the bottom:
else {
$this->_app->enqueueMessage(JText::_('Invalid Email Address'),'error');
$this->setRedirect( 'index.php?option=com_resetpassword' );
}
If $this->_app isn't set, you should be able to get that by using this instead:
JFactory::getApplication()->enqueueMessage(JText::_('Invalid Email Address'),'error');

Categories