I am working on a site in codeigniter.I am not so expert using framework.Here I have to check if email already exists in database.I have coded the required functionality but I am getting an error when submit the form.
In controller i have made the following rule.
My code is:
$this->form_validation->set_rules(
'email', 'Email', 'trim|required|valid_email|is_unique|callback_isEmailExist'
);
public function isEmailExist($email) {
$this->load->library('form_validation');
$this->load->model('user');
$is_exist = $this->user->isEmailExist($email);
if ($is_exist) {
$this->form_validation->set_message(
'isEmailExist', 'Email address is already exist.'
);
return false;
} else {
return true;
}
}
in model user the function is :
function isEmailExist($email) {
$this->db->select('id');
$this->db->where('email', $email);
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
When I submit the form, I am getting the following errors.
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: libraries/Form_validation.php
Line Number: 953
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ` = 'muraddnw@gmail.com' LIMIT 1' at line 2
SELECT * WHERE ` = 'muraddnw@gmail.com' LIMIT 1
Filename: C:\xampp\htdocs\aunction\system\database\DB_driver.php
Line Number: 330
Anyone help me please.
Thanks in advance.
Your problem is in the set_rules line. Here you use both is_unique
and a callback
function. You have to use anyone of that. If you use a call_back function to check duplicate data; doesn't need to use is_unique. For this wrong you get that error
Just remove the is_unique
from there.
$this->form_validation->set_rules('email','Email','trim|required|valid_email|callback_isEmailExist'); // removed is_unique
Try this out and let me know.
$this->form_validation->set_rules('username', 'userName', 'required|callback_exists_username');
#uniqueness of username
function exists_username($str)
{
$record_id = $this->input->post('record_id');
$condition = array('user_id !='=>$record_id,'username'=>$str);
$value =GetAllRecord('user_master',$condition,$is_single=true);
if (count($value) == 0)
{
return TRUE;
}
else
{
$this->form_validation->set_message('exists_username', 'username already exists!');
return FALSE;
}
}
Just add is_unique[tablename.fieldname]
rules to the form validation. for Example:
array('field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|valid_email|is_unique[users.email]'));
Your Model is like
$this->db->select('id');
$this->db->where('email', $email);
please tell the mysql from which table you want to get the record.
$this->db->from('tablename');
it would be like
$this->db->select('id');
$this->db->from('tablename');
$this->db->where('email', $email);
You have an error in the mysql query:-
SELECT * WHERE ` = 'muraddnw@gmail.com' LIMIT 1
Your code has not specified which table to query from. So use following:-
$this->db->select('id');
$this->db->from('tablename');
$this->db->where('email', $email);
Hope this helps.
no need to a make a special function, this is enough
$this->form_validation->set_rules(
'email', 'Email', 'trim|required|valid_email|is_unique'
);
Use is_unique[table.field]
add table name and field.
$this->form_validation->set_rules(
'email', 'Email', 'trim|required|valid_email|is_unique[table.field]|callback_isEmailExist'
);
you can check if a field is unique or not by using form_validator function is_unique[table_name.field_name]